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 E9222CE7AFD for ; Thu, 28 Sep 2023 06:17:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 40CBF8D0081; Thu, 28 Sep 2023 02:17:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3BBAE8D0002; Thu, 28 Sep 2023 02:17:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2A95D8D0081; Thu, 28 Sep 2023 02:17:18 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 1B3A68D0002 for ; Thu, 28 Sep 2023 02:17:18 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id E182F80828 for ; Thu, 28 Sep 2023 06:17:17 +0000 (UTC) X-FDA: 81284998914.04.19EC807 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf03.hostedemail.com (Postfix) with ESMTP id 08CA620008 for ; Thu, 28 Sep 2023 06:17:15 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=eaKPAt1s; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf03.hostedemail.com: domain of rppt@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=rppt@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695881836; a=rsa-sha256; cv=none; b=YMe+DU2LY8iRgCB9D+c9U2VbKvCCcQucwvciNZSvyQEmAwLeXqMsQgoeDeHNN/6is1hRPG 5LQl2jr5kT2k7TVwv09Scy9ZF69PGPCmR6OtXxUysKFYJ0Vb/GGTzj97NqkQ3ndwwjJVfM NostnbRjnmVKXcrfWTJqeqdZCMovSrE= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=eaKPAt1s; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf03.hostedemail.com: domain of rppt@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=rppt@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1695881836; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=V3RnTAObVXAxQtMAIli+j8zAYZLGF7xOCQ5jkePF1N4=; b=1PzMgOdOKXXTxfQ4fLhHRBVdsJjos9PFcDGlTFdwfSj+emo92zLNXFIN4d3Uh2esg4ObbG U+qKsyKbTpA4dHM8HwegbueJ+7bdZgZW04fCmASXUtOeNZj+TeSZZRozPIV0bHmoxuJZS0 D3OeMYR6oH0HJFD1YxkjbPHcemJbZVc= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by ams.source.kernel.org (Postfix) with ESMTP id 81AB4B815E0; Thu, 28 Sep 2023 06:17:14 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 42109C433C7; Thu, 28 Sep 2023 06:17:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1695881833; bh=ukat4gmI11KeKATmcvfP0ZvCM+9QAk5epXbZZWdQNfQ=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=eaKPAt1sY0P8Dj6RhjFZZXKiB73ZiPj1b1lm2m8PR8XRC42+M7Pc2GTSYp7wAo+rL c0M/kcy/w/OSYXT9ZjF+s4WfpxwA1l3SDpUfg4pAUSEfz2cdxFsILqvfpfWXznUH77 icyk50oQLP4a+tcu41CxixgOzv161zjVzt+pbpNbyvor/r5nnpU4H+qPWeEBBCF/U7 lSV3CZXNeU+xIsD2BByogESSqUb7iQWP55I49Cv6iZoxaAiBe1j1H7qV54zsAGK6Ki lgiWxAZjbbhkL4EaDy0tjQEFwjQseM+Ab1rVOK87YhUJTsYM73wSd9TMeAFJilv/Ni TIYZETpht74GQ== Date: Thu, 28 Sep 2023 09:16:19 +0300 From: Mike Rapoport To: Yajun Deng Cc: akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] memblock: don't run loop in memblock_add_range() twice Message-ID: <20230928061619.GS3303@kernel.org> References: <20230927013752.2515238-1-yajun.deng@linux.dev> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230927013752.2515238-1-yajun.deng@linux.dev> X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 08CA620008 X-Stat-Signature: kph6rn75549wqbjgynt7kgeba8xqwubg X-HE-Tag: 1695881835-502635 X-HE-Meta: U2FsdGVkX1/pN+XAoPKuUCH9SAJjrJk5w88c5Nn/odi9WjSF7pwe5Y0LhPKLv6sxnI3qD2ECcTA2Yp0lffixQkaUaWqFdR06cPy+C5O7UdpS+S56O3cj7yh51Y4sv48YAUIKI0bRezdZ4U5ZPc/dSW/ZhFIEKlySnC1FNDhhYod98+yb0AMXjDtKAzXPJZ0dMfp3liAX5As/WG9pTPak08rI/UdZt5oeih08Tpfu6OJ8tQY/HnBq9wXuYsY0mkUkpM200GLSlhKEuq1cIO3kYGJiqSyljAVV8rjxtPPY/REuuSpqswU7fETbBDl08eLGWz31H8fB3Xo1uOoWpXvZff7vKRppeSTKfCY53nBZvpZJxG++V1li5FxIzdKthqrMCqg3QAC8AupuX4gWnfn6snYPHvLv1CGYopXUuCq7ffa+4rdsDFDlHW7p+uRI/eo9NdB3O08e7H+ogUI2o0m00StEl0KTHfAEoBTHwUTDFijpkwG0Fx5BOVjZRbEhrsyoN4YwK2is32UPugOE9kUnZtjXXH0Oep9+eTtdQH0v5fA6frJ8K6iHXvkxUi0fGr7caJ2mqPB7hiiwVdQM8SAqdF7whKJQHcnYqHEcK8FaPjVaBL1rdn4hatONbE4956StodOGnfirDSwZlXa31BTqWtYCk9xpMWutYAHKzNuRjWo4MznSgmYSkETikHFDz24oHJ6L5t5LpnS1itv8b32K+2HDWTOiNrLGWws6P1n09VdEV6DH1jrVRMaIplOBzV3kuwrACYYy9llrqBFcE0k4fpWqK9P6NldQ1ujpUnkQYddd2LCMLv88c05A72+eHRbwAutPKa8LDQzTmidF+s5b7di/RUXbPRusxo2nOL8NN+zGxWa3DMNzL2vHlMkAsV3nGgAj31RnpQ4FYz6APR/9c6HXaTWQmKVt2pQMXLRjXt+shdmY6UDECF6AGlIWgndpS3atcZ0wblIwkpocf0a Q+GYO0WO LjCPSs09gRhbIHpDRfvdyTgtpqQ9/9IRFo5hybOTCW/+1ujpoKt3UINNXGvTkk5/ba7XAd+mYAR0EBvSJqyueiHplqi2dB2Ga5MmYDeQiqCUMEzS9Idrlc+dMtSPp0HxxXPZeW8mowCM3C/Ou7y9Fa/FMFT8EHm4wHGka4S9bpgSPNWbfPmDkMQ7uCGEcW4Qo21TjEg3vyc51lpSr7Nj7SYtPEyLVsyK6gEEoUosB8PiD2YQWZFf7hwwCkQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Wed, Sep 27, 2023 at 09:37:52AM +0800, 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 calling memblock_insert_region(). If the count is > equal to the maximum value, it needs to resize the array. Otherwise, > insert it directly. > > To avoid nested calls to memblock_add_range(), we need to call > memblock_reserve() out of memblock_double_array(). memblock_add_range() does an extra loop once in a while, but I don't think removing it will have any actual effect on the boot time. > Signed-off-by: Yajun Deng > --- > mm/memblock.c | 117 ++++++++++++++++++++++++-------------------------- > 1 file changed, 57 insertions(+), 60 deletions(-) > > diff --git a/mm/memblock.c b/mm/memblock.c > index 5a88d6d24d79..3f44c84f5d0b 100644 > --- a/mm/memblock.c > +++ b/mm/memblock.c > @@ -400,6 +400,8 @@ void __init memblock_discard(void) > * @type: memblock type of the regions array being doubled > * @new_area_start: starting address of memory range to avoid overlap with > * @new_area_size: size of memory range to avoid overlap with > + * @new_reserve_base: starting address of new array > + * @new_reserve_size: size of new array > * > * Double the size of the @type regions array. If memblock is being used to > * allocate memory for a new reserved regions array and there is a previously > @@ -412,7 +414,9 @@ void __init memblock_discard(void) > */ > static int __init_memblock memblock_double_array(struct memblock_type *type, > phys_addr_t new_area_start, > - phys_addr_t new_area_size) > + phys_addr_t new_area_size, > + phys_addr_t *new_reserve_base, > + phys_addr_t *new_reserve_size) > { > struct memblock_region *new_array, *old_array; > phys_addr_t old_alloc_size, new_alloc_size; > @@ -490,11 +494,13 @@ static int __init_memblock memblock_double_array(struct memblock_type *type, > memblock_free(old_array, old_alloc_size); > > /* > - * Reserve the new array if that comes from the memblock. Otherwise, we > - * needn't do it > + * Keep the address and size if that comes from the memblock. Otherwise, > + * we needn't do it. > */ > - if (!use_slab) > - BUG_ON(memblock_reserve(addr, new_alloc_size)); > + if (!use_slab) { > + *new_reserve_base = addr; > + *new_reserve_size = new_alloc_size; > + } > > /* Update slab flag */ > *in_slab = use_slab; > @@ -588,11 +594,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; > + phys_addr_t new_base = 0, new_size; > + int idx, start_rgn = -1, end_rgn; > struct memblock_region *rgn; > + unsigned long ocnt = type->cnt; > > if (!size) > return 0; > @@ -608,25 +615,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 +632,23 @@ 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 ((type->cnt == type->max) && > + memblock_double_array(type, obase, size, > + &new_base, &new_size)) > + return -ENOMEM; > + > + 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 +656,28 @@ 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) && > + memblock_double_array(type, obase, size, > + &new_base, &new_size)) > + return -ENOMEM; > + > + if (start_rgn == -1) > + start_rgn = idx; > + end_rgn = idx + 1; > + memblock_insert_region(type, idx, base, end - base, > + nid, flags); > } > > - if (!nr_new) > + if (ocnt == type->cnt) > 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, start_rgn, end_rgn); > - return 0; > - } > + memblock_merge_regions(type, start_rgn, end_rgn); > + > + /* Reserve the new array */ > + if (new_base) > + memblock_reserve(new_base, new_size); > + > + return 0; > } > > /** > @@ -755,6 +746,7 @@ static int __init_memblock memblock_isolate_range(struct memblock_type *type, > int *start_rgn, int *end_rgn) > { > phys_addr_t end = base + memblock_cap_size(base, &size); > + phys_addr_t new_base = 0, new_size; > int idx; > struct memblock_region *rgn; > > @@ -764,10 +756,15 @@ static int __init_memblock memblock_isolate_range(struct memblock_type *type, > return 0; > > /* we'll create at most two more regions */ > - while (type->cnt + 2 > type->max) > - if (memblock_double_array(type, base, size) < 0) > + if (type->cnt + 2 > type->max) { > + if (memblock_double_array(type, base, size, > + &new_base, &new_size)) > return -ENOMEM; > > + if (new_base) > + memblock_reserve(new_base, new_size); > + } > + > for_each_memblock_type(idx, type, rgn) { > phys_addr_t rbase = rgn->base; > phys_addr_t rend = rbase + rgn->size; > -- > 2.25.1 > -- Sincerely yours, Mike.