linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mm/swapfile.c: simplify the scan loop in scan_swap_map_slots()
@ 2020-02-29 13:15 Wei Yang
  2020-02-29 22:06 ` Wei Yang
  0 siblings, 1 reply; 2+ messages in thread
From: Wei Yang @ 2020-02-29 13:15 UTC (permalink / raw)
  To: akpm; +Cc: linux-mm, linux-kernel, Wei Yang, Hugh Dickins

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>
---
 mm/swapfile.c | 26 +++++++++-----------------
 1 file changed, 9 insertions(+), 17 deletions(-)

diff --git a/mm/swapfile.c b/mm/swapfile.c
index 95024f9b691a..42c5c2010bfc 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -729,6 +729,14 @@ 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)
+{
+	if (++(*offset) > si->highest_bit)
+		*offset = si->lowest_bit;
+	return *offset;
+}
+
 static int scan_swap_map_slots(struct swap_info_struct *si,
 			       unsigned char usage, int nr,
 			       swp_entry_t slots[])
@@ -883,7 +891,7 @@ static int scan_swap_map_slots(struct swap_info_struct *si,
 
 scan:
 	spin_unlock(&si->lock);
-	while (++offset <= si->highest_bit) {
+	while (next_offset(si, &offset) != scan_base) {
 		if (!si->swap_map[offset]) {
 			spin_lock(&si->lock);
 			goto checks;
@@ -897,22 +905,6 @@ static int scan_swap_map_slots(struct swap_info_struct *si,
 			latency_ration = LATENCY_LIMIT;
 		}
 	}
-	offset = si->lowest_bit;
-	while (offset < scan_base) {
-		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++;
-	}
 	spin_lock(&si->lock);
 
 no_page:
-- 
2.23.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] mm/swapfile.c: simplify the scan loop in scan_swap_map_slots()
  2020-02-29 13:15 [PATCH] mm/swapfile.c: simplify the scan loop in scan_swap_map_slots() Wei Yang
@ 2020-02-29 22:06 ` Wei Yang
  0 siblings, 0 replies; 2+ messages in thread
From: Wei Yang @ 2020-02-29 22:06 UTC (permalink / raw)
  To: Wei Yang; +Cc: akpm, linux-mm, linux-kernel, Hugh Dickins

On Sat, Feb 29, 2020 at 01:15:37PM +0000, Wei Yang wrote:
>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>
>---
> mm/swapfile.c | 26 +++++++++-----------------
> 1 file changed, 9 insertions(+), 17 deletions(-)
>
>diff --git a/mm/swapfile.c b/mm/swapfile.c
>index 95024f9b691a..42c5c2010bfc 100644
>--- a/mm/swapfile.c
>+++ b/mm/swapfile.c
>@@ -729,6 +729,14 @@ 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)
>+{
>+	if (++(*offset) > si->highest_bit)
>+		*offset = si->lowest_bit;

Hmm... I found one potential problem here. If someone has eaten the lower
part, (si->lowest_bit > scan_base), we would fall into infinite loop.

Will wait for some comment before sending v2.

>+	return *offset;
>+}
>+
> static int scan_swap_map_slots(struct swap_info_struct *si,
> 			       unsigned char usage, int nr,
> 			       swp_entry_t slots[])
>@@ -883,7 +891,7 @@ static int scan_swap_map_slots(struct swap_info_struct *si,
> 
> scan:
> 	spin_unlock(&si->lock);
>-	while (++offset <= si->highest_bit) {
>+	while (next_offset(si, &offset) != scan_base) {
> 		if (!si->swap_map[offset]) {
> 			spin_lock(&si->lock);
> 			goto checks;
>@@ -897,22 +905,6 @@ static int scan_swap_map_slots(struct swap_info_struct *si,
> 			latency_ration = LATENCY_LIMIT;
> 		}
> 	}
>-	offset = si->lowest_bit;
>-	while (offset < scan_base) {
>-		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++;
>-	}
> 	spin_lock(&si->lock);
> 
> no_page:
>-- 
>2.23.0

-- 
Wei Yang
Help you, Help me

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-02-29 22:06 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-29 13:15 [PATCH] mm/swapfile.c: simplify the scan loop in scan_swap_map_slots() Wei Yang
2020-02-29 22:06 ` Wei Yang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).