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 673FEE936FF for ; Thu, 5 Oct 2023 05:21:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EA17E6B02A3; Thu, 5 Oct 2023 01:21:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E52776B02A4; Thu, 5 Oct 2023 01:21:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D194D6B02A5; Thu, 5 Oct 2023 01:21:08 -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 C2A446B02A3 for ; Thu, 5 Oct 2023 01:21:08 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 93B78C0281 for ; Thu, 5 Oct 2023 05:21:08 +0000 (UTC) X-FDA: 81310259016.01.33EB05B Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf18.hostedemail.com (Postfix) with ESMTP id B14281C0025 for ; Thu, 5 Oct 2023 05:21:06 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=fNJFsH5p; spf=pass (imf18.hostedemail.com: domain of rppt@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=rppt@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1696483267; 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=LVYVB4c+khkEDfwEcvb4dgFV0FETVhi6ONBrYRTD1js=; b=wFXsuZyg6fhjjsGzUW7DJryRctSmXc6tY2y0yvHw/norYH/ET2Ck6b/nsP3rRlL8GFF95A 5jgJ67omVAkze+SjCKyTpEegrT+gxtsikknasGEkPHcup61P0YiJPFZ0BUoFddE3yP2Ti2 lNH/OSZNNLbtt0ZdRFBEBSjCtA2e/IM= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1696483267; a=rsa-sha256; cv=none; b=pbdVKUy8T1kdsEFlWzZGco7hJUGqR5RAE0NXoDOywgGXH+Jq4w4+BcLh+5VBxy6sXWsYZU 3eQNmlWMnJZZPW4NHz5ZK0BAm9Dp28t2+AoUJbwBjsSQSx4xRi4RcN++YfotsuN/uJ6rEM gijJsw/LhdURosIbu5njtdqW0Mu8jG4= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=fNJFsH5p; spf=pass (imf18.hostedemail.com: domain of rppt@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=rppt@kernel.org; dmarc=pass (policy=none) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by ams.source.kernel.org (Postfix) with ESMTP id 1FC94B80159; Thu, 5 Oct 2023 05:21:05 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id F0865C433D9; Thu, 5 Oct 2023 05:21:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1696483263; bh=Qs4busUPBNGM91WL2UH8yfpZT3a7HgW1HG6pgpfmcRA=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=fNJFsH5p9c+uR7GS34KCmB/3qXqajGl4JgK7gXkzguFeyULnUsIbJpItYLHtlhWcC XI2dVoLVmFkeYkynqktUio6MexCUiERvWBlm2Myuu1Pne62gSWUKKVlp5/+N6G+GaB DE+nor54oqUJMfivCjzEI733al48+ZDpe17M15URxExTmQhiixC1vWHXF7YMFVf6m8 Ay1Mr7Ro35G59TP9RL1vI5OsZr/pKUC1D6822PxxA80waokFeIu1Bvm5cYdur9LMwd iY0WChPvASZ3UtwvhHr4+gaFghk9/bs9/fFeTzeD6CuExVp76P37tr/kF1yWpWwZTE cGH9Aty1Thy0w== Date: Thu, 5 Oct 2023 08:19:59 +0300 From: Mike Rapoport To: Yajun Deng Cc: akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v3] memblock: don't run loop in memblock_add_range() twice Message-ID: <20231005051959.GC3303@kernel.org> References: <20231003163045.191184-1-yajun.deng@linux.dev> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20231003163045.191184-1-yajun.deng@linux.dev> X-Stat-Signature: uebuxkhepoiijw7t5xz9hdrnfoofwgbn X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: B14281C0025 X-Rspam-User: X-HE-Tag: 1696483266-767844 X-HE-Meta: U2FsdGVkX1/UJ5s+CVPv5fCWVqoV8FM9DVbcDEUQc77RTzeM211Jz5Lw3WDGQS7m1U1asIptjtxEk3t1YI7hlloCYvaVTVoIW6GzGAtsy6/DgpRYktbF+5jPIfMzyAIqR2ix8v8YA6i2HYpEwn4ZIZoy2rlQD1xwa3WP+KJTzcNZeufXWhes6Jy9gyD0xAPzwHKEiHctUEHV7B9Z86xqzv8N2EsVXT+r4xddDfuZ7zMXiqBbY3cwJh0XX8V3Dj1o1zSD95oD3YyvFU9Xs2nITig0knaEnZmNxpBZHWqpAgAfl28U2kNNcRLpC7/XbMYIxpnAfzSK0BuUxUeOUpXMRYJFahX2wevGigX8/Sod39QaTSevkkZWT0DinHKWO1Oitl3tqb+wb7GO5jZBPOj/3upa8Wev3tKsmWam/pg61pmVk/t8+t+rACfKyLdu5BAY1XP7ey7FuQ4LLEiY0gq0//ItWw6p8LLpL1h7wvmUXevAeynM3/S+iCO5EAVFJc2wd9hmJzah5pITN/RTF1+FL956apXMgpOpSJfWSOoHuaH8+qykm2s5xK+LF7o+Qsuw/RbIAg23/bw70YqxmeQFJmuUrZKzyvhk4NR2I5JHJc7+X2ca2HvVLEx23hPSIMDd9Qk64qJaXO7qwWsA93bI37RQHewd64iVc8EiJYpzkimD/bqxe+P89VKbFMiZpKSAOgnd9fMhEZiNnWgr7e5KcLAf0wi9CRghC+lhzswawXK/80QkImOCLvh9M31YrEeJBQCiHAfcHQ21aSwI72cV2jQKRnQkY1itECTopoRlLe2dnelDzLnsVHbGBxvvlaWs6oUrPfHM3lJDKaP4QvYUbBjMixwwENiLBIiaJed4ZOe36ar/hkqDpqJmB3RDZ+MXy0X5B0udMxb2uiJbp2r0+7RqKoZM+uYBlN/ZdsKjgelo5Kk3yUu6xbfA1pgpbfkbzNgbSMTP9n+HLTsB+SL OEfu/fir QWCQBY6A0j4GIRsfj1esoxd2EETM8wHEtYygo/rOH5bc0uEtGN4MV1KQTW7Y+fEZj7GybkvtPwe/l/0QRdSmNhImfAAtOK7roVy5p7l9VXhPH9349RGoDIfpSusYvwVqYFDFyCWDfBP0wYettlXKZNRS01pE1aEunac/kgyPc1RY3FdRHhRT82PyPKZ7S4SgxEs5ENt6cHJ65uVgh3wPM7iJHmMFp+CAWyLhwgfWK0xZT/dpUzuU8zF2oGlTa03R5/6akI6QZiu4viYQeOMhCFzob4MYe3+dtvm/FfuZGmSMsGaM= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000011, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Wed, Oct 04, 2023 at 12:30:45AM +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 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. I presume this fixes a bug you found in v2, but are you sure it'll _never_ explode on a machine with different memory layout and different sequence of memblock_reservee() calls? I don't see this micro-optimization is worth the churn and potential bugs. NAK. > 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; > } > > /** > -- > 2.25.1 > -- Sincerely yours, Mike.