From mboxrd@z Thu Jan 1 00:00:00 1970 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 EAED4E784AC for ; Mon, 2 Oct 2023 10:57:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0D2498D0017; Mon, 2 Oct 2023 06:57:21 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 05A5A8D0001; Mon, 2 Oct 2023 06:57:20 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E64798D0017; Mon, 2 Oct 2023 06:57:20 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id D41098D0001 for ; Mon, 2 Oct 2023 06:57:20 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id A325280207 for ; Mon, 2 Oct 2023 10:57:20 +0000 (UTC) X-FDA: 81300219840.03.3653538 Received: from out-209.mta1.migadu.com (out-209.mta1.migadu.com [95.215.58.209]) by imf13.hostedemail.com (Postfix) with ESMTP id 70B7720028 for ; Mon, 2 Oct 2023 10:57:17 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=cD0MgmhC; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf13.hostedemail.com: domain of yajun.deng@linux.dev designates 95.215.58.209 as permitted sender) smtp.mailfrom=yajun.deng@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1696244237; a=rsa-sha256; cv=none; b=hmXnGJfd4ZNFi5wspscrtetunpBzhB1a87nwVnH96FuC09ukOKKi1WxjSrhPZWHAqulEnn d5vOjMi+6FfmenoA/PxNVdZ2vk8qtc8xQ+z4K/ROTTVubIhA/H2wQAZ3G4ho8mkSmvsQYd miJQ4NOm1CmKs4auQ7Mdfy//2VA+zwE= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=cD0MgmhC; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf13.hostedemail.com: domain of yajun.deng@linux.dev designates 95.215.58.209 as permitted sender) smtp.mailfrom=yajun.deng@linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1696244237; 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:references:dkim-signature; bh=aQO3XkBHu5rCLD4ypD1Z6J4F2frqkYY0S4aXHFwX8tQ=; b=RQLOyb3U3nBEGRYFoGOBe04D1D4c0D5TMAbrkRfwH3PmlJrFHMEaKY8J4C1JBMafkTYYXl rf+vF4Jjmh3rtGqzyKdfR3oJ6T60uO/C0CTaVBA7K7WsCJ33yWIbNZdCmJtaxcxlnOK1KE FliLrKdMx5C3ksW0w+jvY4exyQ+KOHw= 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: 8bit X-Migadu-Flow: FLOW_OUT X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 70B7720028 X-Stat-Signature: f14p5641wdtk31u669qk5d8nwz9irncd X-HE-Tag: 1696244237-146769 X-HE-Meta: U2FsdGVkX1+QrNhjlhyc0jNCO8m9VN3RSlh8YToUwdjYGOYjf1c0r7/Lk+HTqbiXrIlsmyf0Yzx70YHDTx6/Zh2zxaEKOUCRtJFtbV1fAmKpyrAwHk+usjK3j1D964ZfxzTWpJ+z4t2HY+fpOjTFuSMLNf/cIO2Zwjj2h90mUXYtrSoXytDDnVXL8ukcmClR1LoZQIMkM5b8iKkv9ZT5EcmGKq4mnUokkzECao8xLIKu5n3UG3GVLLn2Hzj28ourotG2M7uUO/Wctfu9YxvNCgP1lqIWLTB6pPNX8J64PfIjMCLzS/puAXdx7bDT+eKdjM2ZLjvmjpHpmsAEc2gylHKOASpPruErlv37W2EgEm4mQ+LhnlP0OzVvYgL8CwiDkOdNhXO26GiMZEZEXQkC02gP39pXLQ2wm7ZSjF07RInsZc2sNwRFqP+clduM6csmkwjpHSIC50do5RmUQ5rqCts+H6OOVSVH+Zn2NbA0Ns2yjSSBaei6JHsQp1uL4pi1HVF0WfkS3a4ZQ8RugHDyPvUwTeFe7fUBGd6vibEID0yIkNncKcw1TF5WwN5SuaeyG3iaXCjndhKgQlSkhu/Tg4USGjzUWFGqwiYt7CFqM/6i/orvbU+vAjE0iiOF8RMv6CXBmn19Xbd6bgmmj3JmmsnTAOMzW9m19nxkbctMJgnD99KftyQrXDy3+i2xBfKGaOD45f/gKjch+Atvowt8TV7xs98npFwAY8aSquu5hU8LPClVJv5YWbDZJtPVXU95IABMf5ew6tMPxVl0GELz6NbRRQ/2dfYMcATGfkCFBWlv7z17+yf7dBoLWOAsuxt43VST22OV1+2DepeYDm8X/H219AI+ZG6i/gn4aQQwVwpfFuq+2RX4ox8e3AsXKorUfgUXsIW/oklYOmSdunrz0mZPEnZlHWQ5I7fT5BFy0HhY14oE1y3jIEZtSYTr7gDi9Q99b26aZjun0lCKXL7 s9ORF7oZ 7HCespcBp+Lu0to+Kt7BYEoefxZva5j2vWNDB6AKmVYVbxzAtP3uRY6lZuYbLaGgnsRYvjsbbiasf/JYJKdACAFxil/7HbfQPNC3afWreM5AEdgtZcHAeQFpzctNjI7APa4KID1oMqULLv2Z6o3UJKjAF64CNEdhcdTspDbtZ6194L1h0YTuRDPnw8g== X-Bogosity: Ham, tests=bogofilter, spamicity=0.001170, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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; } /** -- 2.25.1