From patchwork Tue Oct 3 16:30:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yajun Deng X-Patchwork-Id: 13407775 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 E3CF3E7AD78 for ; Tue, 3 Oct 2023 16:31:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 620868D007F; Tue, 3 Oct 2023 12:31:13 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5D0B98D0003; Tue, 3 Oct 2023 12:31:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 498958D007F; Tue, 3 Oct 2023 12:31:13 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 38ED58D0003 for ; Tue, 3 Oct 2023 12:31:13 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 149BD1A040B for ; Tue, 3 Oct 2023 16:31:13 +0000 (UTC) X-FDA: 81304690026.11.04D5636 Received: from out-190.mta1.migadu.com (out-190.mta1.migadu.com [95.215.58.190]) by imf02.hostedemail.com (Postfix) with ESMTP id 5A16D80017 for ; Tue, 3 Oct 2023 16:31:06 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=TwocuYdY; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf02.hostedemail.com: domain of yajun.deng@linux.dev designates 95.215.58.190 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=1696350667; 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=NJl+SD8R37eFOrXZ1l+okjOAETzKwOwWaRxJv78u12M=; b=g5f/ovZwLCKqJtQSouSSaswDt7zpIXLCaAaNKJOotzrnktcoA8/ve9QMqCeIpi1z6Yi3ev JwBd7BdHYAZoVj6lXfQsXLpxplyElopdOp5hifJHGFPDSLLlSzFJx+y5c0cyXxdPXHffuN qZsXGJSK+eiwNsW9TTYGqkwLYjLTHJc= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=TwocuYdY; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf02.hostedemail.com: domain of yajun.deng@linux.dev designates 95.215.58.190 as permitted sender) smtp.mailfrom=yajun.deng@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1696350667; a=rsa-sha256; cv=none; b=0ud0TyflWtJKX5vok4PmPrUq2A8oxCHQ3CMpP6up3CwPSiUond6ucEKE96s1/70OmiQG9s e40udXzvoSPqPHmdAiXRHCqr2039jl0G6ulvAAdLCdo/G1vIzcEuwwr0WC/riCngRXX0KE SqngGuN0kf+B3yLr2xKRFFpnFEFCAC4= 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=1696350665; 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=NJl+SD8R37eFOrXZ1l+okjOAETzKwOwWaRxJv78u12M=; b=TwocuYdYMlro3aGLYJPrdidoxA7phm/DW/sDlYiP/nmVycs2Zn8vw3sxQZcnc5S9rlZ7pb qiEr/g4pojwuKJrQwKidrjMA8Jtxsq6tNa6ATyxn/zG++L1I8qATkHMNa1l6b8+NLlseBn D9ENZHT3D/ReUFolazSxjXESWtXBP7c= 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 v3] memblock: don't run loop in memblock_add_range() twice Date: Wed, 4 Oct 2023 00:30:45 +0800 Message-Id: <20231003163045.191184-1-yajun.deng@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 5A16D80017 X-Stat-Signature: xxoij3ihewrz5dte6nknbxqkwhotqmi5 X-Rspam-User: X-HE-Tag: 1696350666-474316 X-HE-Meta: U2FsdGVkX181qhQ7HLhchjTTcesaMCudvLHsw22CYGpuJ3/N01vW8hhvkpJhFDz9LnXtTjAg85lBhd9dwkGeGpRm1QjTSsY3ysTOE4UzAsgwOxebQX6SW6nz8+SpNeofC9PhX8ewTou+H6F6h6yKJR8ToOAFNU2D++B7KXYjiuS3LtWR9XV7KTDyxkNtFNQ+2vrbhBI8Wg67qf4kzbkdB8vVN5Dp4S3N3GZ8MnT9yts0fAOsIhkRL0aD8p6QIB8apqniRHLRCroBKfN/IzlLgYCP4QXBubYHBccAhVwgwZwcOu1+xyZ+YnGGSfRaUe+EUR6ORCB2fDFznls26h4tFwbk0gceCq4E7SrdbGpZVwULuPXidozLg/OOZ3f8H2xITm5x8lMJdOBlO3lyntJY7hIVuOqLNuQorLvRBtSwCzxiMEo1kcJVrqPjpofDJB5epZuPG9w90hFnIxyvXehSfpqeC0BALk7AcXeEgewqfs1UOfnZD1b5GggNc23DYMVl+WaoXigGGYxy3L2qxRcnb9vDN9vf9bQe3317Z+6X4ukqBR4zRLhtfTZt2mHbZPGjmQm1pUWPbzj+gry6D5LJkUMUQDJlHhGCzQnuStZmeSymEFA6fbVA5uBuG2cGjOJQKL6hyzjoyKwbRv4OIHnM9x14UQ8/mON+pUEBcGks8cJD56WkrbPv8EIgtl9IVOS55Ekh0njFp5K/p/ycRKf8TI9De3g3pBQefjAxHjIMt+e7nkeU4nd4BE4lFoHIpaVLqNGO8I0rjkJB6bmRS6rWE94kh26oHwCpC4EgI9arH79ZD5Y87koR2HsApMHmsmv4t0UUOxsW/SZGS9ph2ZTdxR/SsrRLaYwyeK9MoZj2r/f3Ll1s8Gi5o4zE58Qh0y34sH1Ud5qNz7qkIRmBrgFBTOSnIkuELP224dJSiD28WPcS8VaempKXDOgH83xe/ZqldXa9+fJvjOm3TMdheP2 hfTW77b+ DtywDVRJfsvJ52eL7ufcOienMsXXJHVZliyiW1yP1vFdzrLpLqKZQO+H8c1kVLRF7SmEdUyAAOR2981yDbxbPsGDZc4TiEsklJajpOXWG0tCkKfvKLmhY/BDXZ6mXfBG5E56LDXNAriFaFTyt3htWjY+LUY/NYp0g9IxP6SFZZiQcA8fz/NKQWr55ZbJYwocU9IlvxLcnBCZvre78lL41biOHAwRZzazhmdAb X-Bogosity: Ham, tests=bogofilter, spamicity=0.005524, 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 memblock_insert_region. If the count is equal to the maximum, it needs to resize the array. Otherwise, insert it directly. Also, there is a nested call here, we need to reserve the current array immediately if slab is unavailable. Signed-off-by: Yajun Deng --- v3: reserve the current array immediately if slab is unavailable. v2: remove the changes of memblock_double_array. v1: https://lore.kernel.org/all/20230927013752.2515238-1-yajun.deng@linux.dev/ --- mm/memblock.c | 93 +++++++++++++++++++++++---------------------------- 1 file changed, 41 insertions(+), 52 deletions(-) diff --git a/mm/memblock.c b/mm/memblock.c index 5a88d6d24d79..71449c0b8bc8 100644 --- a/mm/memblock.c +++ b/mm/memblock.c @@ -588,11 +588,12 @@ 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; + int use_slab = slab_is_available(); + unsigned long ocnt = type->cnt; if (!size) return 0; @@ -608,25 +609,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 or equal to 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; - for_each_memblock_type(idx, type, rgn) { phys_addr_t rbase = rgn->base; phys_addr_t rend = rbase + rgn->size; @@ -644,15 +626,30 @@ 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 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 slab is unavailable, it means a new array was reserved + * in memblock_double_array. There is a nested call here, We + * need to reserve the current array now if its type is + * reserved. + */ + if (type->cnt == type->max) { + if (memblock_double_array(type, obase, size)) + return -ENOMEM; + else if (!use_slab && type == &memblock.reserved) + return memblock_reserve(obase, size); } + + 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 +657,25 @@ 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 (type->cnt == type->max) { + if (memblock_double_array(type, obase, size)) + return -ENOMEM; + else if (!use_slab && type == &memblock.reserved) + return memblock_reserve(obase, size); } - } - 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; } /**