linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Yuntao Wang <ytcoode@gmail.com>
To: dennis@kernel.org
Cc: akpm@linux-foundation.org, cl@linux.com,
	linux-kernel@vger.kernel.org, linux-mm@kvack.org, tj@kernel.org,
	ytcoode@gmail.com
Subject: [PATCH v2] percpu: simplify the logic of pcpu_alloc_first_chunk()
Date: Wed, 24 Apr 2024 16:27:30 +0800	[thread overview]
Message-ID: <20240424082730.99945-1-ytcoode@gmail.com> (raw)
In-Reply-To: <ZiiUoC5NokdpvDgN@snowbird>

In the logic for hiding the end region of the chunk, there are several
places where the same value 'region_bits - offset_bits' is calculated
over and over again using different methods. Eliminating these redundant
calculations can improve code readability.

Additionally, there is a lot of repetitive code when hiding the start and
end regions of the chunk. We can consolidate this logic into a function,
making the final code cleaner and more concise.

Signed-off-by: Yuntao Wang <ytcoode@gmail.com>
---
v1 -> v2: Optimize the code based on Dennis's suggestion

 mm/percpu.c | 27 ++++++++++++---------------
 1 file changed, 12 insertions(+), 15 deletions(-)

diff --git a/mm/percpu.c b/mm/percpu.c
index 4e11fc1e6def..d22b317f3d41 100644
--- a/mm/percpu.c
+++ b/mm/percpu.c
@@ -1329,6 +1329,15 @@ static void pcpu_init_md_blocks(struct pcpu_chunk *chunk)
 		pcpu_init_md_block(md_block, PCPU_BITMAP_BLOCK_BITS);
 }
 
+static void pcpu_chunk_hide_region(struct pcpu_chunk *chunk, int bit_off,
+				   int bits)
+{
+	bitmap_set(chunk->alloc_map, bit_off, bits);
+	set_bit(bit_off, chunk->bound_map);
+	set_bit(bit_off + bits, chunk->bound_map);
+	pcpu_block_update_hint_alloc(chunk, bit_off, bits);
+}
+
 /**
  * pcpu_alloc_first_chunk - creates chunks that serve the first chunk
  * @tmp_addr: the start of the region served
@@ -1409,27 +1418,15 @@ static struct pcpu_chunk * __init pcpu_alloc_first_chunk(unsigned long tmp_addr,
 	if (chunk->start_offset) {
 		/* hide the beginning of the bitmap */
 		offset_bits = chunk->start_offset / PCPU_MIN_ALLOC_SIZE;
-		bitmap_set(chunk->alloc_map, 0, offset_bits);
-		set_bit(0, chunk->bound_map);
-		set_bit(offset_bits, chunk->bound_map);
-
 		chunk->chunk_md.first_free = offset_bits;
-
-		pcpu_block_update_hint_alloc(chunk, 0, offset_bits);
+		pcpu_chunk_hide_region(chunk, 0, offset_bits);
 	}
 
 	if (chunk->end_offset) {
 		/* hide the end of the bitmap */
 		offset_bits = chunk->end_offset / PCPU_MIN_ALLOC_SIZE;
-		bitmap_set(chunk->alloc_map,
-			   pcpu_chunk_map_bits(chunk) - offset_bits,
-			   offset_bits);
-		set_bit((start_offset + map_size) / PCPU_MIN_ALLOC_SIZE,
-			chunk->bound_map);
-		set_bit(region_bits, chunk->bound_map);
-
-		pcpu_block_update_hint_alloc(chunk, pcpu_chunk_map_bits(chunk)
-					     - offset_bits, offset_bits);
+		pcpu_chunk_hide_region(chunk, region_bits - offset_bits,
+				       offset_bits);
 	}
 
 	return chunk;
-- 
2.44.0



  reply	other threads:[~2024-04-24  8:27 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-23 13:55 [PATCH] " Yuntao Wang
2024-04-24  5:12 ` Dennis Zhou
2024-04-24  8:27   ` Yuntao Wang [this message]
2024-05-14  1:25   ` Yuntao Wang
2024-05-14  1:30     ` Dennis Zhou

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=20240424082730.99945-1-ytcoode@gmail.com \
    --to=ytcoode@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=cl@linux.com \
    --cc=dennis@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=tj@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