2024-04-23 13:58:11

by Yuntao Wang

[permalink] [raw]
Subject: [PATCH] percpu: simplify the logic of pcpu_alloc_first_chunk()

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

Signed-off-by: Yuntao Wang <[email protected]>
---
mm/percpu.c | 12 +++++-------
1 file changed, 5 insertions(+), 7 deletions(-)

diff --git a/mm/percpu.c b/mm/percpu.c
index 4e11fc1e6def..2a051f00f68d 100644
--- a/mm/percpu.c
+++ b/mm/percpu.c
@@ -1421,15 +1421,13 @@ static struct pcpu_chunk * __init pcpu_alloc_first_chunk(unsigned long tmp_addr,
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);
+ start_offset = region_bits - offset_bits;
+
+ bitmap_set(chunk->alloc_map, start_offset, offset_bits);
+ set_bit(start_offset, 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_block_update_hint_alloc(chunk, start_offset, offset_bits);
}

return chunk;
--
2.44.0



2024-04-24 05:12:12

by Dennis Zhou

[permalink] [raw]
Subject: Re: [PATCH] percpu: simplify the logic of pcpu_alloc_first_chunk()

Hi Yuntao,

On Tue, Apr 23, 2024 at 09:55:25PM +0800, Yuntao Wang wrote:
> In the logic for hiding the end of the bitmap, there are several places
> where the same value 'region_bits - offset_bits' is calculated over and
> over again using different methods. Eliminate these redundant calculations
> to improve code readability.
>
> Signed-off-by: Yuntao Wang <[email protected]>
> ---
> mm/percpu.c | 12 +++++-------
> 1 file changed, 5 insertions(+), 7 deletions(-)
>
> diff --git a/mm/percpu.c b/mm/percpu.c
> index 4e11fc1e6def..2a051f00f68d 100644
> --- a/mm/percpu.c
> +++ b/mm/percpu.c
> @@ -1421,15 +1421,13 @@ static struct pcpu_chunk * __init pcpu_alloc_first_chunk(unsigned long tmp_addr,
> 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);
> + start_offset = region_bits - offset_bits;

Generally I think this makes sense, but I'm less inclined to mix the
start_offset variable name in here.

A helper function might make this cleaner, consolidating the
start_offset and end_offset logic.

static void pcpu_chunk_hide_region(chunk, bit_off, bits);

Thanks,
Dennis

> +
> + bitmap_set(chunk->alloc_map, start_offset, offset_bits);
> + set_bit(start_offset, 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_block_update_hint_alloc(chunk, start_offset, offset_bits);
> }
>
> return chunk;
> --
> 2.44.0
>


2024-04-24 08:33:42

by Yuntao Wang

[permalink] [raw]
Subject: [PATCH v2] percpu: simplify the logic of pcpu_alloc_first_chunk()

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 <[email protected]>
---
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


2024-05-14 01:25:17

by Yuntao Wang

[permalink] [raw]
Subject: Re: [PATCH] percpu: simplify the logic of pcpu_alloc_first_chunk()

Hi Dennis,

Can this v2 version of the patch be merged? Or what else do I need to do?

Thanks,
Yuntao

2024-05-14 01:31:10

by Dennis Zhou

[permalink] [raw]
Subject: Re: [PATCH] percpu: simplify the logic of pcpu_alloc_first_chunk()

Hi Yuntao,

On Tue, May 14, 2024 at 09:25:00AM +0800, Yuntao Wang wrote:
> Hi Dennis,
>
> Can this v2 version of the patch be merged? Or what else do I need to do?
>
> Thanks,
> Yuntao

I'm going to massage it just a little bit to format a bit more like the
rest of percpu.c. If it's a little too much, I'll send out a v3 and keep
you as the author, then apply it to for-6.10.

Thanks,
Dennis

2024-05-14 01:56:12

by Yuntao Wang

[permalink] [raw]
Subject: Re: [PATCH] percpu: simplify the logic of pcpu_alloc_first_chunk()

Hi Dennis,

On Mon, 13 May 2024 18:30:57 -0700, Dennis Zhou <[email protected]> wrote:
> Hi Yuntao,
>
> On Tue, May 14, 2024 at 09:25:00AM +0800, Yuntao Wang wrote:
> > Hi Dennis,
> >
> > Can this v2 version of the patch be merged? Or what else do I need to do?
> >
> > Thanks,
> > Yuntao
>
> I'm going to massage it just a little bit to format a bit more like the
> rest of percpu.c. If it's a little too much, I'll send out a v3 and keep
> you as the author, then apply it to for-6.10.
>
> Thanks,
> Dennis

Thanks a lot. :-)