[2/6] kexec: delete a useless check in crash_shrink_memory()

Message ID 20230527123439.772-3-thunder.leizhen@huawei.com
State New
Headers
Series kexec: enable kexec_crash_size to support two crash kernel regions |

Commit Message

Zhen Lei May 27, 2023, 12:34 p.m. UTC
  The check '(crashk_res.parent != NULL)' is added by
commit e05bd3367bd3 ("kexec: fix Oops in crash_shrink_memory()"), but it's
stale now. Because if 'crashk_res' is not reserved, it will be zero in
size and will be intercepted by the above 'if (new_size >= old_size)'.

Ago:
	if (new_size >= end - start + 1)

Now:
	old_size = (end == 0) ? 0 : end - start + 1;
	if (new_size >= old_size)

Signed-off-by: Zhen Lei <thunder.leizhen@huawei.com>
---
 kernel/kexec_core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
  

Comments

Baoquan He May 31, 2023, 12:17 a.m. UTC | #1
On 05/27/23 at 08:34pm, Zhen Lei wrote:
> The check '(crashk_res.parent != NULL)' is added by
> commit e05bd3367bd3 ("kexec: fix Oops in crash_shrink_memory()"), but it's
> stale now. Because if 'crashk_res' is not reserved, it will be zero in
> size and will be intercepted by the above 'if (new_size >= old_size)'.
> 
> Ago:
> 	if (new_size >= end - start + 1)
> 
> Now:
> 	old_size = (end == 0) ? 0 : end - start + 1;
> 	if (new_size >= old_size)

Hmm, I would strongly suggest we keep that check. Even though the
current code like above can do the acutal checking, but its actual usage
is not obvious for checking of crashk_res existence. In the future,
someone may change above calculation and don't notice the hidden
functionality at all behind the calculation. The cost of the check is
almost zero, right?

> 
> Signed-off-by: Zhen Lei <thunder.leizhen@huawei.com>
> ---
>  kernel/kexec_core.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/kernel/kexec_core.c b/kernel/kexec_core.c
> index 22acee18195a591..d1ab139dd49035e 100644
> --- a/kernel/kexec_core.c
> +++ b/kernel/kexec_core.c
> @@ -1137,7 +1137,7 @@ int crash_shrink_memory(unsigned long new_size)
>  	end = start + new_size;
>  	crash_free_reserved_phys_range(end, crashk_res.end);
>  
> -	if ((start == end) && (crashk_res.parent != NULL))
> +	if (start == end)
>  		release_resource(&crashk_res);
>  
>  	ram_res->start = end;
> -- 
> 2.25.1
>
  
Zhen Lei May 31, 2023, 2:19 a.m. UTC | #2
On 2023/5/31 8:17, Baoquan He wrote:
> On 05/27/23 at 08:34pm, Zhen Lei wrote:
>> The check '(crashk_res.parent != NULL)' is added by
>> commit e05bd3367bd3 ("kexec: fix Oops in crash_shrink_memory()"), but it's
>> stale now. Because if 'crashk_res' is not reserved, it will be zero in
>> size and will be intercepted by the above 'if (new_size >= old_size)'.
>>
>> Ago:
>> 	if (new_size >= end - start + 1)
>>
>> Now:
>> 	old_size = (end == 0) ? 0 : end - start + 1;
>> 	if (new_size >= old_size)
> 
> Hmm, I would strongly suggest we keep that check. Even though the
> current code like above can do the acutal checking, but its actual usage
> is not obvious for checking of crashk_res existence. In the future,
> someone may change above calculation and don't notice the hidden
> functionality at all behind the calculation. The cost of the check is
> almost zero, right?

The cost of the check is negligible. The only downside is that it's hard to
understand why it's added, and I only found out why by looking at the history
log. In my opinion, the above 'Now:' is the right fix.

> 
>>
>> Signed-off-by: Zhen Lei <thunder.leizhen@huawei.com>
>> ---
>>  kernel/kexec_core.c | 2 +-
>>  1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/kernel/kexec_core.c b/kernel/kexec_core.c
>> index 22acee18195a591..d1ab139dd49035e 100644
>> --- a/kernel/kexec_core.c
>> +++ b/kernel/kexec_core.c
>> @@ -1137,7 +1137,7 @@ int crash_shrink_memory(unsigned long new_size)
>>  	end = start + new_size;
>>  	crash_free_reserved_phys_range(end, crashk_res.end);
>>  
>> -	if ((start == end) && (crashk_res.parent != NULL))
>> +	if (start == end)
>>  		release_resource(&crashk_res);
>>  
>>  	ram_res->start = end;
>> -- 
>> 2.25.1
>>
> 
> .
>
  
Baoquan He May 31, 2023, 7:41 a.m. UTC | #3
On 05/31/23 at 10:19am, Leizhen (ThunderTown) wrote:
> 
> 
> On 2023/5/31 8:17, Baoquan He wrote:
> > On 05/27/23 at 08:34pm, Zhen Lei wrote:
> >> The check '(crashk_res.parent != NULL)' is added by
> >> commit e05bd3367bd3 ("kexec: fix Oops in crash_shrink_memory()"), but it's
> >> stale now. Because if 'crashk_res' is not reserved, it will be zero in
> >> size and will be intercepted by the above 'if (new_size >= old_size)'.
> >>
> >> Ago:
> >> 	if (new_size >= end - start + 1)
> >>
> >> Now:
> >> 	old_size = (end == 0) ? 0 : end - start + 1;
> >> 	if (new_size >= old_size)
> > 
> > Hmm, I would strongly suggest we keep that check. Even though the
> > current code like above can do the acutal checking, but its actual usage
> > is not obvious for checking of crashk_res existence. In the future,
> > someone may change above calculation and don't notice the hidden
> > functionality at all behind the calculation. The cost of the check is
> > almost zero, right?
> 
> The cost of the check is negligible. The only downside is that it's hard to
> understand why it's added, and I only found out why by looking at the history
> log. In my opinion, the above 'Now:' is the right fix.

It checks if the resource exists before releasing, just a normal
checking?
> 
> > 
> >>
> >> Signed-off-by: Zhen Lei <thunder.leizhen@huawei.com>
> >> ---
> >>  kernel/kexec_core.c | 2 +-
> >>  1 file changed, 1 insertion(+), 1 deletion(-)
> >>
> >> diff --git a/kernel/kexec_core.c b/kernel/kexec_core.c
> >> index 22acee18195a591..d1ab139dd49035e 100644
> >> --- a/kernel/kexec_core.c
> >> +++ b/kernel/kexec_core.c
> >> @@ -1137,7 +1137,7 @@ int crash_shrink_memory(unsigned long new_size)
> >>  	end = start + new_size;
> >>  	crash_free_reserved_phys_range(end, crashk_res.end);
> >>  
> >> -	if ((start == end) && (crashk_res.parent != NULL))
> >> +	if (start == end)
> >>  		release_resource(&crashk_res);
> >>  
> >>  	ram_res->start = end;
> >> -- 
> >> 2.25.1
> >>
> > 
> > .
> > 
> 
> -- 
> Regards,
>   Zhen Lei
>
  
Zhen Lei May 31, 2023, 8:26 a.m. UTC | #4
On 2023/5/31 15:41, Baoquan He wrote:
> On 05/31/23 at 10:19am, Leizhen (ThunderTown) wrote:
>>
>>
>> On 2023/5/31 8:17, Baoquan He wrote:
>>> On 05/27/23 at 08:34pm, Zhen Lei wrote:
>>>> The check '(crashk_res.parent != NULL)' is added by
>>>> commit e05bd3367bd3 ("kexec: fix Oops in crash_shrink_memory()"), but it's
>>>> stale now. Because if 'crashk_res' is not reserved, it will be zero in
>>>> size and will be intercepted by the above 'if (new_size >= old_size)'.
>>>>
>>>> Ago:
>>>> 	if (new_size >= end - start + 1)
>>>>
>>>> Now:
>>>> 	old_size = (end == 0) ? 0 : end - start + 1;
>>>> 	if (new_size >= old_size)
>>>
>>> Hmm, I would strongly suggest we keep that check. Even though the
>>> current code like above can do the acutal checking, but its actual usage
>>> is not obvious for checking of crashk_res existence. In the future,
>>> someone may change above calculation and don't notice the hidden
>>> functionality at all behind the calculation. The cost of the check is
>>> almost zero, right?
>>
>> The cost of the check is negligible. The only downside is that it's hard to
>> understand why it's added, and I only found out why by looking at the history
>> log. In my opinion, the above 'Now:' is the right fix.
> 
> It checks if the resource exists before releasing, just a normal
> checking?

If resource_size(&crashk_res) is zero, it means that crashk_res has not been
added(insert_resource) or has been deleted(release_resource). I've tested it. It's okay.

>>
>>>
>>>>
>>>> Signed-off-by: Zhen Lei <thunder.leizhen@huawei.com>
>>>> ---
>>>>  kernel/kexec_core.c | 2 +-
>>>>  1 file changed, 1 insertion(+), 1 deletion(-)
>>>>
>>>> diff --git a/kernel/kexec_core.c b/kernel/kexec_core.c
>>>> index 22acee18195a591..d1ab139dd49035e 100644
>>>> --- a/kernel/kexec_core.c
>>>> +++ b/kernel/kexec_core.c
>>>> @@ -1137,7 +1137,7 @@ int crash_shrink_memory(unsigned long new_size)
>>>>  	end = start + new_size;
>>>>  	crash_free_reserved_phys_range(end, crashk_res.end);
>>>>  
>>>> -	if ((start == end) && (crashk_res.parent != NULL))
>>>> +	if (start == end)
>>>>  		release_resource(&crashk_res);
>>>>  
>>>>  	ram_res->start = end;
>>>> -- 
>>>> 2.25.1
>>>>
>>>
>>> .
>>>
>>
>> -- 
>> Regards,
>>   Zhen Lei
>>
> 
> 
> .
>
  

Patch

diff --git a/kernel/kexec_core.c b/kernel/kexec_core.c
index 22acee18195a591..d1ab139dd49035e 100644
--- a/kernel/kexec_core.c
+++ b/kernel/kexec_core.c
@@ -1137,7 +1137,7 @@  int crash_shrink_memory(unsigned long new_size)
 	end = start + new_size;
 	crash_free_reserved_phys_range(end, crashk_res.end);
 
-	if ((start == end) && (crashk_res.parent != NULL))
+	if (start == end)
 		release_resource(&crashk_res);
 
 	ram_res->start = end;