From patchwork Wed Oct 19 12:03:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yajun Deng X-Patchwork-Id: 13011644 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 17AF8C4332F for ; Wed, 19 Oct 2022 12:04:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8BCDE6B0072; Wed, 19 Oct 2022 08:04:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 86CA56B0073; Wed, 19 Oct 2022 08:04:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 75C526B0074; Wed, 19 Oct 2022 08:04:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 668A76B0072 for ; Wed, 19 Oct 2022 08:04:44 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 38E43C0A75 for ; Wed, 19 Oct 2022 12:04:44 +0000 (UTC) X-FDA: 80037567288.19.F7BA42B Received: from out2.migadu.com (out2.migadu.com [188.165.223.204]) by imf03.hostedemail.com (Postfix) with ESMTP id 917BC2002C for ; Wed, 19 Oct 2022 12:04:43 +0000 (UTC) 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=1666181081; 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=9Zp0rsOwaZAudeS2Yn3cYhCuuXMlRO2UwNZgHWLJ9K8=; b=Q9+90QS7LNNNm7kvUFJq+pkQ4l+2WZSD6FZFg0q7ZB5/5f5Xs5+3GMbwNjkTuCj7iJOrW5 YUKLmIry915vUhFojJ/KmG4rSx+AD/GffFSwQ4Qra4DtOzTkjNXNHx6VsCv+TPUnXe8fUU tKcSlGnMJLvICa4C26YADM1IV2yUjmQ= 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] memblock: remove repeat round Date: Wed, 19 Oct 2022 20:03:37 +0800 Message-Id: <20221019120337.2098298-1-yajun.deng@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1666181083; a=rsa-sha256; cv=none; b=TzwNBnvS8wWN91EBHmprNWqr+13wA45YBfCP1A44BvQ5z9qoAbJzgnlCElRlGBtpAmXKui XwLriN5Yw9C7IsI6gAevGP17jrsYWoob00VyXOPvR4+EpVw4HAW2nDui/T9aUv/JiInGL5 BN8f1SaeyV1YgA/9VDrFxL2H0/PLtD8= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=Q9+90QS7; spf=pass (imf03.hostedemail.com: domain of yajun.deng@linux.dev designates 188.165.223.204 as permitted sender) smtp.mailfrom=yajun.deng@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1666181083; 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=9Zp0rsOwaZAudeS2Yn3cYhCuuXMlRO2UwNZgHWLJ9K8=; b=S12FgG6HpMcaN5e6aOr6iw8ZbJpHMCjJCpqpmVK1eKs4Oh/btgomvA/I7vKjUAwLwr/sIp xoteHXBXuU7BIrt7MOq0ZEapRAgR9StpFO1Z5t7vRAvIqBmZa0/H5pNzQMTLjlHBnEod0v v4cvplSxAg9HBk+IMqM7SZjrY4ovvJs= X-Rspam-User: Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=Q9+90QS7; spf=pass (imf03.hostedemail.com: domain of yajun.deng@linux.dev designates 188.165.223.204 as permitted sender) smtp.mailfrom=yajun.deng@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-Stat-Signature: 1z9tcocs1ixoknejyomn9fjyg5wqcspu X-Rspamd-Queue-Id: 917BC2002C X-Rspamd-Server: rspam10 X-HE-Tag: 1666181083-767172 X-Bogosity: Ham, tests=bogofilter, spamicity=0.004707, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: There is no need round twice in memblock_add_range(). We can call memblock_double_array() to extand the size if type->cnt no less than type->max before memblock_insert_region(), otherwise we can insert the new region directly. Signed-off-by: Yajun Deng --- mm/memblock.c | 54 +++++++++++++++------------------------------------ 1 file changed, 16 insertions(+), 38 deletions(-) diff --git a/mm/memblock.c b/mm/memblock.c index 511d4783dcf1..1679244b4a1a 100644 --- a/mm/memblock.c +++ b/mm/memblock.c @@ -578,7 +578,6 @@ 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; @@ -598,22 +597,6 @@ static int __init_memblock memblock_add_range(struct memblock_type *type, return 0; } - /* - * 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 type->max, we know - * that there is enough empty regions in @type, and we can insert - * regions 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; @@ -635,10 +618,14 @@ static int __init_memblock memblock_add_range(struct memblock_type *type, #endif WARN_ON(flags != rgn->flags); nr_new++; - if (insert) - memblock_insert_region(type, idx++, base, - rbase - base, nid, - flags); + + if ((type->cnt >= type->max) && + (memblock_double_array(type, obase, size) < 0)) + return -ENOMEM; + + memblock_insert_region(type, idx++, base, + rbase - base, nid, + flags); } /* area below @rend is dealt with, forget about it */ base = min(rend, end); @@ -647,28 +634,19 @@ static int __init_memblock memblock_add_range(struct memblock_type *type, /* insert the remaining portion */ if (base < end) { nr_new++; - if (insert) - memblock_insert_region(type, idx, base, end - base, - nid, flags); + if ((type->cnt >= type->max) && + (memblock_double_array(type, obase, size) < 0)) + return -ENOMEM; + + memblock_insert_region(type, idx, base, end - base, + nid, flags); } if (!nr_new) return 0; - /* - * 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 { - memblock_merge_regions(type); - return 0; - } + memblock_merge_regions(type); + return 0; } /**