Re: [PATCH v2] mm/swapfile.c: simplify the scan loop in scan_swap_map_slots()

Wei Yang
2020-04-29T22:06:24Z

On Wed, Apr 29, 2020 at 08:52:44AM +0800, Huang, Ying wrote:
>Wei Yang <richard.weiyang@gmail.com> writes:
>
>> On Mon, Apr 27, 2020 at 08:55:33AM +0800, Huang, Ying wrote:
>>>Wei Yang <richard.weiyang@gmail.com> writes:
>>>
>>>> On Sun, Apr 26, 2020 at 09:07:11AM +0800, Huang, Ying wrote:
>>>>>Wei Yang <richard.weiyang@gmail.com> writes:
>>>>>
>>>>>> On Fri, Apr 24, 2020 at 10:02:58AM +0800, Huang, Ying wrote:
>>>>>>>Wei Yang <richard.weiyang@gmail.com> writes:
>>>>>>>
>>>>>> [...]
>>>>>>>>>
>>>>>>>>>if "offset > si->highest_bit" is true and "offset < scan_base" is true,
>>>>>>>>>scan_base need to be returned.
>>>>>>>>>
>>>>>>>>
>>>>>>>> When this case would happen in the original code?
>>>>>>>
>>>>>>>In the original code, the loop can still stop.
>>>>>>>
>>>>>>
>>>>>> Sorry, I don't get your point yet.
>>>>>>
>>>>>> In original code, there are two separate loops
>>>>>>
>>>>>> while (++offset <= si->highest_bit) {
>>>>>> }
>>>>>>
>>>>>> while (offset < scan_base) {
>>>>>> }
>>>>>>
>>>>>> And for your condition, (offset > highest_bit) && (offset < scan_base), which
>>>>>> terminates the first loop and fits the second loop well.
>>>>>>
>>>>>> Not sure how this condition would stop the loop in original code?
>>>>>
>>>>>Per my understanding, in your code, if some other task changes
>>>>>si->highest_bit to be less than scan_base in parallel. The loop may
>>>>>cannot stop.
>>>>
>>>> When (offset > scan_base), (offset > si->highest_bit) means offset will be
>>>> set to si->lowest_bit.
>>>>
>>>> When (offset < scan_base), next_offset() would always increase offset till
>>>> offset is scan_base.
>>>>
>>>> Sorry, I didn't catch your case. Would you minding giving more detail?
>>>
>>>Don't think in single thread model. There's no lock to prevent other
>>>tasks to change si->highest_bit simultaneously. For example, task B may
>>>change si->highest_bit to be less than scan_base in task A.
>>>
>>
>> Yes, I am trying to think about it in parallel mode.
>>
>> Here are the cases, it might happen in parallel when task B change highest_bit
>> to be less than scan_base.
>>
>> (1)
>> offset
>> v
>> +-------------------+------------------+
>> ^ ^ ^
>> lowest_bit highest_bit scan_base
>>
>>
>> (2)
>> offset
>> v
>> +-------------------+------------------+
>> ^ ^ ^
>> lowest_bit highest_bit scan_base
>>
>
>This is the case in my mind. But my original understanding to your code
>wasn't correct. As you said, loop can stop because offset is kept
>increasing. Sorry about that.
>

NP.

>But I still don't like your new code. It's not as obvious as the
>original one.

Sure, thanks for your time.

>
>Best Regards,
>Huang, Ying
>
>> (3)
>> offset
>> v
>> +-------------------+------------------+
>> ^ ^ ^
>> lowest_bit highest_bit scan_base
>>
>> Case (1), (offset > highest) && (offset > scan_base), offset would be set to
>> lowest_bit. This looks good.
>>
>> Case (2), (offset > highest) && (offset < scan_base), since offset is less
>> than scan_base, it wouldn't be set to lowest. Instead it will continue to
>> scan_base.
>>
>> Case (3), almost the same as Case (2).
>>
>> In Case (2) and (3), one thing interesting is the loop won't stop at
>> highest_bit, while the behavior is the same as original code.
>>
>> Maybe your concern is this one? I still not figure out your point about the
>> infinite loop. Hope you would share some light on it.
>>
>>
>>>Best Regards,
>>>Huang, Ying
>>>
>>>>>
>>>>>Best Regards,
>>>>>Huang, Ying
>>>>>
>>>>>>>Best Regards,
>>>>>>>Huang, Ying
>>>>>>>
>>>>>>>>>Again, the new code doesn't make it easier to find this kind of issues.
>>>>>>>>>
>>>>>>>>>Best Regards,
>>>>>>>>>Huang, Ying

--
Wei Yang
Help you, Help me