diff mbox series

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

Message ID 20200422214111.19370-1-richard.weiyang@gmail.com (mailing list archive)
State New, archived
Headers show
Series [v2] mm/swapfile.c: simplify the scan loop in scan_swap_map_slots() | expand

Commit Message

Wei Yang April 22, 2020, 9:41 p.m. UTC
After commit c60aa176c6de8 ("swapfile: swap allocation cycle if
nonrot"), swap allocation is cyclic. Current approach is done with two
separate loop on the upper and lower half. This looks a little
redundant.

From another point of view, the loop iterates [lowest_bit, highest_bit]
range starting with (offset + 1) but except scan_base. So we can
simplify the loop with condition (next_offset() != scan_base) by
introducing next_offset() which makes sure offset fit in that range
with correct order.

Signed-off-by: Wei Yang <richard.weiyang@gmail.com>
CC: Hugh Dickins <hughd@google.com>
CC: "Huang, Ying" <ying.huang@intel.com>

---
v2:
  * return scan_base if the lower part is eaten
  * only start over when iterating on the upper part
---
 mm/swapfile.c | 31 ++++++++++++++-----------------
 1 file changed, 14 insertions(+), 17 deletions(-)

Comments

Huang, Ying April 23, 2020, 5:57 a.m. UTC | #1
Wei Yang <richard.weiyang@gmail.com> writes:

> After commit c60aa176c6de8 ("swapfile: swap allocation cycle if
> nonrot"), swap allocation is cyclic. Current approach is done with two
> separate loop on the upper and lower half. This looks a little
> redundant.

I can understand that the redundant code doesn't smell good.  But I
don't think the new code is easier to be understood than the original
one.

> From another point of view, the loop iterates [lowest_bit, highest_bit]
> range starting with (offset + 1) but except scan_base. So we can
> simplify the loop with condition (next_offset() != scan_base) by
> introducing next_offset() which makes sure offset fit in that range
> with correct order.
>
> Signed-off-by: Wei Yang <richard.weiyang@gmail.com>
> CC: Hugh Dickins <hughd@google.com>
> CC: "Huang, Ying" <ying.huang@intel.com>
>
> ---
> v2:
>   * return scan_base if the lower part is eaten
>   * only start over when iterating on the upper part
> ---
>  mm/swapfile.c | 31 ++++++++++++++-----------------
>  1 file changed, 14 insertions(+), 17 deletions(-)
>
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index f903e5a165d5..0005a4a1c1b4 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
> @@ -729,6 +729,19 @@ static void swap_range_free(struct swap_info_struct *si, unsigned long offset,
>  	}
>  }
>  
> +static unsigned long next_offset(struct swap_info_struct *si,
> +				unsigned long *offset, unsigned long scan_base)
> +{
> +	/* only start over when iterating on the upper part */
> +	if (++(*offset) > si->highest_bit && *offset > scan_base) {
> +		*offset = si->lowest_bit;
> +		/* someone has eaten the lower part */
> +		if (si->lowest_bit >= scan_base)
> +			return scan_base;
> +	}

if "offset > si->highest_bit" is true and "offset < scan_base" is true,
scan_base need to be returned.

Again, the new code doesn't make it easier to find this kind of issues.

Best Regards,
Huang, Ying
Wei Yang April 23, 2020, 1:15 p.m. UTC | #2
On Thu, Apr 23, 2020 at 01:57:34PM +0800, Huang, Ying wrote:
>Wei Yang <richard.weiyang@gmail.com> writes:
>
>> After commit c60aa176c6de8 ("swapfile: swap allocation cycle if
>> nonrot"), swap allocation is cyclic. Current approach is done with two
>> separate loop on the upper and lower half. This looks a little
>> redundant.
>
>I can understand that the redundant code doesn't smell good.  But I
>don't think the new code is easier to be understood than the original
>one.
>
>> From another point of view, the loop iterates [lowest_bit, highest_bit]
>> range starting with (offset + 1) but except scan_base. So we can
>> simplify the loop with condition (next_offset() != scan_base) by
>> introducing next_offset() which makes sure offset fit in that range
>> with correct order.
>>
>> Signed-off-by: Wei Yang <richard.weiyang@gmail.com>
>> CC: Hugh Dickins <hughd@google.com>
>> CC: "Huang, Ying" <ying.huang@intel.com>
>>
>> ---
>> v2:
>>   * return scan_base if the lower part is eaten
>>   * only start over when iterating on the upper part
>> ---
>>  mm/swapfile.c | 31 ++++++++++++++-----------------
>>  1 file changed, 14 insertions(+), 17 deletions(-)
>>
>> diff --git a/mm/swapfile.c b/mm/swapfile.c
>> index f903e5a165d5..0005a4a1c1b4 100644
>> --- a/mm/swapfile.c
>> +++ b/mm/swapfile.c
>> @@ -729,6 +729,19 @@ static void swap_range_free(struct swap_info_struct *si, unsigned long offset,
>>  	}
>>  }
>>  
>> +static unsigned long next_offset(struct swap_info_struct *si,
>> +				unsigned long *offset, unsigned long scan_base)
>> +{
>> +	/* only start over when iterating on the upper part */
>> +	if (++(*offset) > si->highest_bit && *offset > scan_base) {
>> +		*offset = si->lowest_bit;
>> +		/* someone has eaten the lower part */
>> +		if (si->lowest_bit >= scan_base)
>> +			return scan_base;
>> +	}
>
>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?

>Again, the new code doesn't make it easier to find this kind of issues.
>
>Best Regards,
>Huang, Ying
Huang, Ying April 24, 2020, 2:02 a.m. UTC | #3
Wei Yang <richard.weiyang@gmail.com> writes:

> On Thu, Apr 23, 2020 at 01:57:34PM +0800, Huang, Ying wrote:
>>Wei Yang <richard.weiyang@gmail.com> writes:
>>
>>> After commit c60aa176c6de8 ("swapfile: swap allocation cycle if
>>> nonrot"), swap allocation is cyclic. Current approach is done with two
>>> separate loop on the upper and lower half. This looks a little
>>> redundant.
>>
>>I can understand that the redundant code doesn't smell good.  But I
>>don't think the new code is easier to be understood than the original
>>one.
>>
>>> From another point of view, the loop iterates [lowest_bit, highest_bit]
>>> range starting with (offset + 1) but except scan_base. So we can
>>> simplify the loop with condition (next_offset() != scan_base) by
>>> introducing next_offset() which makes sure offset fit in that range
>>> with correct order.
>>>
>>> Signed-off-by: Wei Yang <richard.weiyang@gmail.com>
>>> CC: Hugh Dickins <hughd@google.com>
>>> CC: "Huang, Ying" <ying.huang@intel.com>
>>>
>>> ---
>>> v2:
>>>   * return scan_base if the lower part is eaten
>>>   * only start over when iterating on the upper part
>>> ---
>>>  mm/swapfile.c | 31 ++++++++++++++-----------------
>>>  1 file changed, 14 insertions(+), 17 deletions(-)
>>>
>>> diff --git a/mm/swapfile.c b/mm/swapfile.c
>>> index f903e5a165d5..0005a4a1c1b4 100644
>>> --- a/mm/swapfile.c
>>> +++ b/mm/swapfile.c
>>> @@ -729,6 +729,19 @@ static void swap_range_free(struct swap_info_struct *si, unsigned long offset,
>>>  	}
>>>  }
>>>  
>>> +static unsigned long next_offset(struct swap_info_struct *si,
>>> +				unsigned long *offset, unsigned long scan_base)
>>> +{
>>> +	/* only start over when iterating on the upper part */
>>> +	if (++(*offset) > si->highest_bit && *offset > scan_base) {
>>> +		*offset = si->lowest_bit;
>>> +		/* someone has eaten the lower part */
>>> +		if (si->lowest_bit >= scan_base)
>>> +			return scan_base;
>>> +	}
>>
>>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.

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 April 25, 2020, 12:30 a.m. UTC | #4
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?

>Best Regards,
>Huang, Ying
>
>>>Again, the new code doesn't make it easier to find this kind of issues.
>>>
>>>Best Regards,
>>>Huang, Ying
Huang, Ying April 26, 2020, 1:07 a.m. UTC | #5
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.

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 April 26, 2020, 9:19 p.m. UTC | #6
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?

>
>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
Huang, Ying April 27, 2020, 12:55 a.m. UTC | #7
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.

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 April 28, 2020, 9:22 p.m. UTC | #8
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


(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
Huang, Ying April 29, 2020, 12:52 a.m. UTC | #9
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.

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

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 April 29, 2020, 10:06 p.m. UTC | #10
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
diff mbox series

Patch

diff --git a/mm/swapfile.c b/mm/swapfile.c
index f903e5a165d5..0005a4a1c1b4 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -729,6 +729,19 @@  static void swap_range_free(struct swap_info_struct *si, unsigned long offset,
 	}
 }
 
+static unsigned long next_offset(struct swap_info_struct *si,
+				unsigned long *offset, unsigned long scan_base)
+{
+	/* only start over when iterating on the upper part */
+	if (++(*offset) > si->highest_bit && *offset > scan_base) {
+		*offset = si->lowest_bit;
+		/* someone has eaten the lower part */
+		if (si->lowest_bit >= scan_base)
+			return scan_base;
+	}
+	return *offset;
+}
+
 static int scan_swap_map_slots(struct swap_info_struct *si,
 			       unsigned char usage, int nr,
 			       swp_entry_t slots[])
@@ -876,22 +889,7 @@  static int scan_swap_map_slots(struct swap_info_struct *si,
 
 scan:
 	spin_unlock(&si->lock);
-	while (++offset <= si->highest_bit) {
-		if (!si->swap_map[offset]) {
-			spin_lock(&si->lock);
-			goto checks;
-		}
-		if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
-			spin_lock(&si->lock);
-			goto checks;
-		}
-		if (unlikely(--latency_ration < 0)) {
-			cond_resched();
-			latency_ration = LATENCY_LIMIT;
-		}
-	}
-	offset = si->lowest_bit;
-	while (offset < scan_base) {
+	while (next_offset(si, &offset, scan_base) != scan_base) {
 		if (!si->swap_map[offset]) {
 			spin_lock(&si->lock);
 			goto checks;
@@ -904,7 +902,6 @@  static int scan_swap_map_slots(struct swap_info_struct *si,
 			cond_resched();
 			latency_ration = LATENCY_LIMIT;
 		}
-		offset++;
 	}
 	spin_lock(&si->lock);