From nobody Mon Jan 5 11:12:02 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D2C73E784AF for ; Mon, 2 Oct 2023 10:57:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236579AbjJBK5W (ORCPT ); Mon, 2 Oct 2023 06:57:22 -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 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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.d= ev/ --- 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 =3D false; phys_addr_t obase =3D base; phys_addr_t end =3D base + memblock_cap_size(base, &size); - int idx, nr_new, start_rgn =3D -1, end_rgn; + int idx, start_rgn =3D -1, end_rgn; struct memblock_region *rgn; + unsigned long ocnt =3D type->cnt; =20 if (!size) return 0; @@ -609,23 +609,13 @@ static int __init_memblock memblock_add_range(struct = memblock_type *type, } =20 /* - * 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 <=3D type->max) - insert =3D 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 =3D obase; - nr_new =3D 0; + if ((type->cnt =3D=3D type->max) && + memblock_double_array(type, obase, size)) + return -ENOMEM; =20 for_each_memblock_type(idx, type, rgn) { phys_addr_t rbase =3D rgn->base; @@ -644,15 +634,13 @@ static int __init_memblock memblock_add_range(struct = memblock_type *type, WARN_ON(nid !=3D memblock_get_region_node(rgn)); #endif WARN_ON(flags !=3D rgn->flags); - nr_new++; - if (insert) { - if (start_rgn =3D=3D -1) - start_rgn =3D idx; - end_rgn =3D idx + 1; - memblock_insert_region(type, idx++, base, - rbase - base, nid, - flags); - } + + if (start_rgn =3D=3D -1) + start_rgn =3D idx; + end_rgn =3D idx + 1; + memblock_insert_region(type, idx++, base, + rbase - base, nid, + flags); } /* area below @rend is dealt with, forget about it */ base =3D min(rend, end); @@ -660,33 +648,18 @@ static int __init_memblock memblock_add_range(struct = memblock_type *type, =20 /* insert the remaining portion */ if (base < end) { - nr_new++; - if (insert) { - if (start_rgn =3D=3D -1) - start_rgn =3D idx; - end_rgn =3D idx + 1; - memblock_insert_region(type, idx, base, end - base, - nid, flags); - } - } =20 - if (!nr_new) - return 0; + if (start_rgn =3D=3D -1) + start_rgn =3D idx; + end_rgn =3D idx + 1; + memblock_insert_region(type, idx, base, end - base, + nid, flags); + } =20 - /* - * 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 =3D true; - goto repeat; - } else { + if (ocnt !=3D type->cnt) memblock_merge_regions(type, start_rgn, end_rgn); - return 0; - } + + return 0; } =20 /** --=20 2.25.1