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 87D3DE776E9 for ; Tue, 3 Oct 2023 01:11:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E76436B0155; Mon, 2 Oct 2023 21:11:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E26156B0156; Mon, 2 Oct 2023 21:11:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CEE8D6B015A; Mon, 2 Oct 2023 21:11:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id BB3886B0155 for ; Mon, 2 Oct 2023 21:11:05 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 8D58DA013C for ; Tue, 3 Oct 2023 01:11:05 +0000 (UTC) X-FDA: 81302371290.10.4C7FA98 Received: from out-209.mta0.migadu.com (out-209.mta0.migadu.com [91.218.175.209]) by imf08.hostedemail.com (Postfix) with ESMTP id AAAF6160005 for ; Tue, 3 Oct 2023 01:11:01 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=QQzvgV4d; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf08.hostedemail.com: domain of yajun.deng@linux.dev designates 91.218.175.209 as permitted sender) smtp.mailfrom=yajun.deng@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1696295463; a=rsa-sha256; cv=none; b=hlLVJUZOoPSrhyE04WY5XUL0+0Sl40pPpjMiMzEdXin/3XGxatugSXzZ0iGhLWcQ3NlWk4 H+KUzD/THmco1oYQRNrh0QuIDqi4blFQVlyS7mZU80C/UAH5scVekOxqa8qM6WK8sNnKIZ SCH5aV0TZiJRcLVppEJHgXF6xUK43Yg= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=QQzvgV4d; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf08.hostedemail.com: domain of yajun.deng@linux.dev designates 91.218.175.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=1696295463; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=5Y+M8W22DJ+iYV3T19lMhzDKTMIOgxq9NtsM1yyB2PE=; b=mShwCBIZzkY83c58mtSOq8rIciANrDdbfc0knhp9U5acqF9zDfqHj9WUhM314lnG6niGJT Q8Ff0vWNzNYeUEPGpAhToxcOEnoaUfqAJQEgHEQibGhFjGlCmiB+wkByvPgTH0E98Gt6d1 lXcdYq4xCRT69mWiKIul/Wdsk+uQl8U= Message-ID: <685ece42-e62b-86e9-a4b3-805c5d4b9047@linux.dev> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1696295459; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=5Y+M8W22DJ+iYV3T19lMhzDKTMIOgxq9NtsM1yyB2PE=; b=QQzvgV4dExR6cGX025t9kOEfoooPBrxNDMn8PwZ9kEnuUxx+9Zz+CU1gvtrzkpvPvihfou is6tRTIBu1OqNdCYWOczc5JLOyxpz0cQYX/2FumvjlW+dKqOfoKwZt5CYg++3FiUqCnv2e kb9puGjk2ZcfveCW1zBBfWkagVIqLrc= Date: Tue, 3 Oct 2023 09:10:53 +0800 MIME-Version: 1.0 Subject: Re: [PATCH v2] memblock: don't run loop in memblock_add_range() twice Content-Language: en-US To: rppt@kernel.org, akpm@linux-foundation.org Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <20231002105652.2514182-1-yajun.deng@linux.dev> X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Yajun Deng In-Reply-To: <20231002105652.2514182-1-yajun.deng@linux.dev> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_OUT X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: AAAF6160005 X-Stat-Signature: 64qk314ibis97tdma5knx3tti5hb8hba X-HE-Tag: 1696295461-40754 X-HE-Meta: U2FsdGVkX1/NudSmrafpSoQejbEX8S6XZ9F5sdKumS/oR5Jewm2dJm7JMv5EPSIK5Wubzi24hNy3xfHbwBicNJsnMSzoY64OKzNbNisT+aBxF4ksGkax6il8eyveJxNP/tQZzJs7NSPQX7dhQ/67DQnLGMp5uCvVNvO1xA7z/Vj7rgZu3XRML/bYbJe3kDmbwD54xKZiUz/O40BaN5IDlC2/4npPF2D/D9NfxXcwouG5jxiMliuckMUSuYaBfQPJokxdY7I++nsWCJQ17pA9f7HypH++eBcHGkNwSS7r3cfKPLnkSI6BgPyUZf6QHKKF4NlQAmK3PY7MZdKK04imV3PqB6UsA+rbKtjOELYN+RX7Puihbs81Bwajmp3cZCD2FtYF4NykM5QB5/l4dWFwkt9YhJ2VlVzpdufmrmUxTMbQ4Nk0WmUu/JscZKnrczaHMJ7c/lCtI5ZWIfE2SEOLVZQCw4CEaozcjcbfawFOWcjUPFYaui3zsbSLjSelH2GfkDS1+u3S3Emg6SBAYQmJu2GHcMil3VR1tjKsG2u+3FjWmSvB2MKL48FZMfI3G5ZSFZ5V7yAlIiEHWBTZ5C4F/jQKA1PQFKR3llA2OuRjaRjntzWZVGBijMN2GxhMRhZkoLtTmGLfyMRgVA22lVSwKWnSzXH3zqhMsdeOtrlj1CPsAYZ5m3MqEdzfWDh5MZ7EDTF9Ys2zt2rzO2h7Tu6rVA1sYlV8tJRNq9BRDVYFEIab9SvXuLKRR+BqTaGNUnUCB3aq2K+XvMEqa0rte6uS3qNUXHlMCclQQ65yGFLC8muT8nCNeMHw6E3csLdXicZclF1oK2DAUP0s4/lU+izSVUwy+B92hc7KwDpEnXZX2cXBj9qdZ4UJyJ4gCo70vOBIPsGf/EhTZ928m37G24PZTvE7ZJgv2XD6jX4Cgg2fsPXeuZkb7g7nNRCiD/LRq4B/9NyqRc0+w8dUZ8+cL4Y ym6Hot5K SaHbcb6/1RZbQETV3v6T5xs2nzaQNaDsDkqJV2/Mqhb1RBnFfmzqwIEFo+4FNDfY9CTSHBOwiKdUbPHSHd6GqsBwsZZc8Cn//7yEOiMPSKGp3wgyuJU6l58YjxJwrPKtvzWYciiOjE4ZutibK02FJT5WCDST/fjQqROStwMn/KShyxsc+uLUIxMpbakZxRuMA6W0W X-Bogosity: Ham, tests=bogofilter, spamicity=0.000196, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Sorry, there is a bug in this version, please ignore it. On 2023/10/2 18:56, Yajun Deng wrote: > 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; > } > > /**