[PATCH 1/2] drivers/base/memory: Simplify add_boot_memory_block()

Gavin Shan posted 2 patches 10 months, 1 week ago
There is a newer version of this series
[PATCH 1/2] drivers/base/memory: Simplify add_boot_memory_block()
Posted by Gavin Shan 10 months, 1 week ago
It's unnecessary to keep the variable @section_count in the function
because the device for the specific memory block will be added if
any of its memory section is present. The variable @section_count
records the number of present memory sections in the specific memory
block, which isn't needed.

Simplify the function by dropping the variable @section_count. No
functional change intended.

Signed-off-by: Gavin Shan <gshan@redhat.com>
---
 drivers/base/memory.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)

diff --git a/drivers/base/memory.c b/drivers/base/memory.c
index 348c5dbbfa68..208b9b544012 100644
--- a/drivers/base/memory.c
+++ b/drivers/base/memory.c
@@ -822,18 +822,17 @@ static int add_memory_block(unsigned long block_id, unsigned long state,
 
 static int __init add_boot_memory_block(unsigned long base_section_nr)
 {
-	int section_count = 0;
 	unsigned long nr;
 
 	for (nr = base_section_nr; nr < base_section_nr + sections_per_block;
-	     nr++)
-		if (present_section_nr(nr))
-			section_count++;
+	     nr++) {
+		if (present_section_nr(nr)) {
+			return add_memory_block(memory_block_id(base_section_nr),
+						MEM_ONLINE, NULL, NULL);
+		}
+	}
 
-	if (section_count == 0)
-		return 0;
-	return add_memory_block(memory_block_id(base_section_nr),
-				MEM_ONLINE, NULL,  NULL);
+	return 0;
 }
 
 static int add_hotplug_memory_block(unsigned long block_id,
-- 
2.48.1
Re: [PATCH 1/2] drivers/base/memory: Simplify add_boot_memory_block()
Posted by Andrew Morton 10 months, 1 week ago
On Fri, 14 Feb 2025 16:35:03 +1000 Gavin Shan <gshan@redhat.com> wrote:

> It's unnecessary to keep the variable @section_count in the function
> because the device for the specific memory block will be added if
> any of its memory section is present. The variable @section_count
> records the number of present memory sections in the specific memory
> block, which isn't needed.
> 
> Simplify the function by dropping the variable @section_count. No
> functional change intended.
> 
> ...
>
>  static int __init add_boot_memory_block(unsigned long base_section_nr)
>  {
> -	int section_count = 0;
>  	unsigned long nr;
>  
>  	for (nr = base_section_nr; nr < base_section_nr + sections_per_block;

mm/sparse.c has a for_each_present_section_nr() - is that usable here?

> -	     nr++)
> -		if (present_section_nr(nr))
> -			section_count++;
> +	     nr++) {
> +		if (present_section_nr(nr)) {
> +			return add_memory_block(memory_block_id(base_section_nr),
> +						MEM_ONLINE, NULL, NULL);
> +		}
> +	}
>
> ...
>
Re: [PATCH 1/2] drivers/base/memory: Simplify add_boot_memory_block()
Posted by Gavin Shan 10 months, 1 week ago
On 2/15/25 8:57 AM, Andrew Morton wrote:
> On Fri, 14 Feb 2025 16:35:03 +1000 Gavin Shan <gshan@redhat.com> wrote:
> 
>> It's unnecessary to keep the variable @section_count in the function
>> because the device for the specific memory block will be added if
>> any of its memory section is present. The variable @section_count
>> records the number of present memory sections in the specific memory
>> block, which isn't needed.
>>
>> Simplify the function by dropping the variable @section_count. No
>> functional change intended.
>>
>> ...
>>
>>   static int __init add_boot_memory_block(unsigned long base_section_nr)
>>   {
>> -	int section_count = 0;
>>   	unsigned long nr;
>>   
>>   	for (nr = base_section_nr; nr < base_section_nr + sections_per_block;
> 
> mm/sparse.c has a for_each_present_section_nr() - is that usable here?
> 

It should be fine to use it. I will add one preparatory patch to expose
for_each_present_section_nr(). With it, the nested if statements can also
be avoided, Something like below.

         for_each_present_section_nr(base_section_nr - 1, nr) {
		if (nr >= base_section_nr + sections_per_block)
			break;

		return add_memory_block(memory_block_id(nr), MEM_ONLINE, NULL, NULL);
         }

	return 0;

>> -	     nr++)
>> -		if (present_section_nr(nr))
>> -			section_count++;
>> +	     nr++) {
>> +		if (present_section_nr(nr)) {
>> +			return add_memory_block(memory_block_id(base_section_nr),
>> +						MEM_ONLINE, NULL, NULL);
>> +		}
>> +	}
>>

Thanks,
Gavin
Re: [PATCH 1/2] drivers/base/memory: Simplify add_boot_memory_block()
Posted by David Hildenbrand 10 months, 1 week ago
On 14.02.25 07:35, Gavin Shan wrote:
> It's unnecessary to keep the variable @section_count in the function
> because the device for the specific memory block will be added if
> any of its memory section is present. The variable @section_count
> records the number of present memory sections in the specific memory
> block, which isn't needed.
> 
> Simplify the function by dropping the variable @section_count. No
> functional change intended.
> 
> Signed-off-by: Gavin Shan <gshan@redhat.com>
> ---
>   drivers/base/memory.c | 15 +++++++--------
>   1 file changed, 7 insertions(+), 8 deletions(-)
> 
> diff --git a/drivers/base/memory.c b/drivers/base/memory.c
> index 348c5dbbfa68..208b9b544012 100644
> --- a/drivers/base/memory.c
> +++ b/drivers/base/memory.c
> @@ -822,18 +822,17 @@ static int add_memory_block(unsigned long block_id, unsigned long state,
>   
>   static int __init add_boot_memory_block(unsigned long base_section_nr)
>   {
> -	int section_count = 0;
>   	unsigned long nr;
>   
>   	for (nr = base_section_nr; nr < base_section_nr + sections_per_block;
> -	     nr++)
> -		if (present_section_nr(nr))
> -			section_count++;
> +	     nr++) {
> +		if (present_section_nr(nr)) {
> +			return add_memory_block(memory_block_id(base_section_nr),
> +						MEM_ONLINE, NULL, NULL);
> +		}

Superfluous set of braces for the if statement.

Not sure I count this while thing here as a "simplifcation" -- the code 
is IMHO easier to read without the nested return in the loop body.

No strong opinion, though.

-- 
Cheers,

David / dhildenb
Re: [PATCH 1/2] drivers/base/memory: Simplify add_boot_memory_block()
Posted by Gavin Shan 10 months, 1 week ago
On 2/14/25 5:53 PM, David Hildenbrand wrote:
> On 14.02.25 07:35, Gavin Shan wrote:
>> It's unnecessary to keep the variable @section_count in the function
>> because the device for the specific memory block will be added if
>> any of its memory section is present. The variable @section_count
>> records the number of present memory sections in the specific memory
>> block, which isn't needed.
>>
>> Simplify the function by dropping the variable @section_count. No
>> functional change intended.
>>
>> Signed-off-by: Gavin Shan <gshan@redhat.com>
>> ---
>>   drivers/base/memory.c | 15 +++++++--------
>>   1 file changed, 7 insertions(+), 8 deletions(-)
>>
>> diff --git a/drivers/base/memory.c b/drivers/base/memory.c
>> index 348c5dbbfa68..208b9b544012 100644
>> --- a/drivers/base/memory.c
>> +++ b/drivers/base/memory.c
>> @@ -822,18 +822,17 @@ static int add_memory_block(unsigned long block_id, unsigned long state,
>>   static int __init add_boot_memory_block(unsigned long base_section_nr)
>>   {
>> -    int section_count = 0;
>>       unsigned long nr;
>>       for (nr = base_section_nr; nr < base_section_nr + sections_per_block;
>> -         nr++)
>> -        if (present_section_nr(nr))
>> -            section_count++;
>> +         nr++) {
>> +        if (present_section_nr(nr)) {
>> +            return add_memory_block(memory_block_id(base_section_nr),
>> +                        MEM_ONLINE, NULL, NULL);
>> +        }
> 
> Superfluous set of braces for the if statement.
> 
> Not sure I count this while thing here as a "simplifcation" -- the code is IMHO easier to read without the nested return in the loop body.
> 
> No strong opinion, though.
> 

Indeed. I will use for_each_present_section_nr() as Andrew suggested. With it,
one level of the nested if statement can be avoided. The point was to avoid
counting the number of present sections in the specified block since the block
will be added if any section is present.

Thanks,
Gavin