From: Yajun Deng <yajun.deng@linux.dev>
To: rppt@kernel.org, akpm@linux-foundation.org
Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] memblock: don't run loop in memblock_add_range() twice
Date: Tue, 3 Oct 2023 09:10:53 +0800 [thread overview]
Message-ID: <685ece42-e62b-86e9-a4b3-805c5d4b9047@linux.dev> (raw)
In-Reply-To: <20231002105652.2514182-1-yajun.deng@linux.dev>
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 <yajun.deng@linux.dev>
> ---
> 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;
> }
>
> /**
next prev parent reply other threads:[~2023-10-03 1:11 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-02 10:56 Yajun Deng
2023-10-03 1:10 ` Yajun Deng [this message]
-- strict thread matches above, loose matches on Subject: below --
2023-10-03 1:19 Yeah Yeah
2022-10-25 7:09 Yajun Deng
2022-10-31 8:16 ` Mike Rapoport
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=685ece42-e62b-86e9-a4b3-805c5d4b9047@linux.dev \
--to=yajun.deng@linux.dev \
--cc=akpm@linux-foundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=rppt@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox