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

Wei Yang
2020-04-23T13:15:12Z

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

--
Wei Yang
Help you, Help me