Skip to contentSkip to conversation

mm/swapfile.c: found_free could be represented by (tmp < max)

PATCH Superseded Wei Yang 2 participants 9 messages Apr 19, 2020 – Apr 20, 2020

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Wei Yang

author
#0 lore raw

PATCH 1/4 mm/swapfile.c: found_free could be represented by (tmp < max)

This is not necessary to use the variable found_free to record the status. Just check tmp and max is enough.

---
 mm/swapfile.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)
mm/swapfile.c +3 -8
index c457f3be6944..654bad5173bc 100644
@@ -601,7 +601,6 @@ static bool scan_swap_map_try_ssd_cluster(struct swap_info_struct *si,
 {
 	struct percpu_cluster *cluster;
 	struct swap_cluster_info *ci;
-	bool found_free;
 	unsigned long tmp, max;
 
 new_cluster:
@@ -623,8 +622,6 @@ static bool scan_swap_map_try_ssd_cluster(struct swap_info_struct *si,
 			return false;
 	}
 
-	found_free = false;
-
 	/*
 	 * Other CPUs can use our cluster if they can't find a free cluster,
 	 * check if there is still free entry in the cluster
@@ -638,21 +635,19 @@ static bool scan_swap_map_try_ssd_cluster(struct swap_info_struct *si,
 	}
 	ci = lock_cluster(si, tmp);
 	while (tmp < max) {
-		if (!si->swap_map[tmp]) {
-			found_free = true;
+		if (!si->swap_map[tmp])
 			break;
-		}
 		tmp++;
 	}
 	unlock_cluster(ci);
-	if (!found_free) {
+	if (tmp >= max) {
 		cluster_set_null(&cluster->index);
 		goto new_cluster;
 	}
 	cluster->next = tmp + 1;
 	*offset = tmp;
 	*scan_base = tmp;
-	return found_free;
+	return tmp < max;
 }
 
 static void __del_from_avail_list(struct swap_info_struct *p)
Signature · 1 line
2.23.0
author
#1 lore raw

PATCH 2/4 mm/swapfile.c: tmp is always smaller than max

If tmp is bigger or equal to max, we would jump to new_cluster.

Return true directly.

---
 mm/swapfile.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/swapfile.c +1 -1
index 654bad5173bc..3aae700f9931 100644
@@ -647,7 +647,7 @@ static bool scan_swap_map_try_ssd_cluster(struct swap_info_struct *si,
 	cluster->next = tmp + 1;
 	*offset = tmp;
 	*scan_base = tmp;
-	return tmp < max;
+	return true;
 }
 
 static void __del_from_avail_list(struct swap_info_struct *p)
Signature · 1 line
2.23.0
author
#6 lore raw

PATCH 4/4 mm/swapfile.c: move new_cluster to check free_clusters directly

In reply to Wei Yang

Each time it needs jump to new_cluster, it is sure current percpu_cluster is null.

Move the new_cluster to check free_clusters directly.

---
 mm/swapfile.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/swapfile.c +1 -1
index 07b0bc095411..78e92ff14c79 100644
@@ -603,9 +603,9 @@ static bool scan_swap_map_try_ssd_cluster(struct swap_info_struct *si,
 	struct swap_cluster_info *ci;
 	unsigned long tmp, max;
 
-new_cluster:
 	cluster = this_cpu_ptr(si->percpu_cluster);
 	if (cluster_is_null(&cluster->index)) {
+new_cluster:
 		if (!cluster_list_empty(&si->free_clusters)) {
 			cluster->index = si->free_clusters.head;
 			cluster->next = cluster_next(&cluster->index) *
Signature · 1 line
2.23.0
author
#2 lore raw

PATCH 3/4 mm/swapfile.c: compare tmp and max after trying to iterate on swap_map

In reply to Wei Yang

There are two duplicate code to handle the case when there is no available swap entry. Just let the code go through and do the check at second place.

No functional change is expected.

---
 mm/swapfile.c | 4 ----
 1 file changed, 4 deletions(-)
mm/swapfile.c +0 -4
index 3aae700f9931..07b0bc095411 100644
@@ -629,10 +629,6 @@ static bool scan_swap_map_try_ssd_cluster(struct swap_info_struct *si,
 	tmp = cluster->next;
 	max = min_t(unsigned long, si->max,
 		    (cluster_next(&cluster->index) + 1) * SWAPFILE_CLUSTER);
-	if (tmp >= max) {
-		cluster_set_null(&cluster->index);
-		goto new_cluster;
-	}
 	ci = lock_cluster(si, tmp);
 	while (tmp < max) {
 		if (!si->swap_map[tmp])
Signature · 1 line
2.23.0
#3 lore raw

Re: [PATCH 3/4] mm/swapfile.c: compare tmp and max after trying to iterate on swap_map

Quoted 16 lines · Wei Yang

There are two duplicate code to handle the case when there is no available swap entry. Just let the code go through and do the check at second place.

No functional change is expected.

---
 mm/swapfile.c | 4 ----
 1 file changed, 4 deletions(-)
mm/swapfile.c +0 -0
index 3aae700f9931..07b0bc095411 100644

mm/swapfile.c static bool scan_swap_map_try_ssd_cluster(struct swap_info_struct *si, · line 635

Quoted 7 lines
mm/swapfile.c +0 -4
@@ -629,10 +629,6 @@ static bool scan_swap_map_try_ssd_cluster(struct swap_info_struct *si,
 	tmp = cluster->next;
 	max = min_t(unsigned long, si->max,
 		    (cluster_next(&cluster->index) + 1) * SWAPFILE_CLUSTER);
-	if (tmp >= max) {
-		cluster_set_null(&cluster->index);
-		goto new_cluster;
-	}

The code is to avoid to acquire the cluster lock unnecessarily. So I think we should keep this.

Best Regards,
Huang, Ying

Quoted 3 lines
mm/swapfile.c +0 -0
 	ci = lock_cluster(si, tmp);
 	while (tmp < max) {
 		if (!si->swap_map[tmp])
#7 lore raw

Re: [PATCH 4/4] mm/swapfile.c: move new_cluster to check free_clusters directly

In reply to Wei Yang
Quoted 15 lines · Wei Yang

Each time it needs jump to new_cluster, it is sure current percpu_cluster is null.

Move the new_cluster to check free_clusters directly.

---
 mm/swapfile.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/swapfile.c +0 -0
index 07b0bc095411..78e92ff14c79 100644

mm/swapfile.c static bool scan_swap_map_try_ssd_cluster(struct swap_info_struct *si, · line 611

Quoted 10 lines
mm/swapfile.c +1 -1
@@ -603,9 +603,9 @@ static bool scan_swap_map_try_ssd_cluster(struct swap_info_struct *si,
 	struct swap_cluster_info *ci;
 	unsigned long tmp, max;
 
-new_cluster:
 	cluster = this_cpu_ptr(si->percpu_cluster);
 	if (cluster_is_null(&cluster->index)) {
+new_cluster:
 		if (!cluster_list_empty(&si->free_clusters)) {
 			cluster->index = si->free_clusters.head;
 			cluster->next = cluster_next(&cluster->index) *

In swap_do_scheduled_discard(), we will unlock si->lock, so the percpu_cluster may be changed after we releasing the lock. Or the current thread may be moved to a different CPU.

Best Regards,
Huang, Ying

author
#4 lore raw

Re: [PATCH 3/4] mm/swapfile.c: compare tmp and max after trying to iterate on swap_map

In reply to Huang, Ying
Quoted 13 lines · Ying
Quoted 11 lines · Wei Yang

There are two duplicate code to handle the case when there is no available swap entry. Just let the code go through and do the check at second place.

No functional change is expected.

---
 mm/swapfile.c | 4 ----
 1 file changed, 4 deletions(-)
Quoted 16 lines

mm/swapfile.c static bool scan_swap_map_try_ssd_cluster(struct swap_info_struct *si, · line 635

Quoted 12 lines
mm/swapfile.c +0 -4
index 3aae700f9931..07b0bc095411 100644
@@ -629,10 +629,6 @@ static bool scan_swap_map_try_ssd_cluster(struct swap_info_struct *si,
 	tmp = cluster->next;
 	max = min_t(unsigned long, si->max,
 		    (cluster_next(&cluster->index) + 1) * SWAPFILE_CLUSTER);
-	if (tmp >= max) {
-		cluster_set_null(&cluster->index);
-		goto new_cluster;
-	}

The code is to avoid to acquire the cluster lock unnecessarily. So I think we should keep this.

If you really want to avoid the lock, my suggestion is to add:

  if (tmp < max) {
      ci = lock_cluster(si, tmp);
          while (tmp < max) {
	  ...
	  }
      unlock_cluster(ci);

}

Instead of do the similar thing twice.

Quoted 6 lines

Best Regards,
Huang, Ying

Quoted 3 lines
mm/swapfile.c +0 -0
 	ci = lock_cluster(si, tmp);
 	while (tmp < max) {
 		if (!si->swap_map[tmp])
Signature · 2 lines
Wei Yang
Help you, Help me
author
#8 lore raw

Re: [PATCH 4/4] mm/swapfile.c: move new_cluster to check free_clusters directly

In reply to Huang, Ying
Quoted 20 lines · Ying
Quoted 18 lines · Wei Yang

Each time it needs jump to new_cluster, it is sure current percpu_cluster is null.

Move the new_cluster to check free_clusters directly.

---
 mm/swapfile.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/swapfile.c +0 -0
index 07b0bc095411..78e92ff14c79 100644
@@ -603,9 +603,9 @@ static bool scan_swap_map_try_ssd_cluster(struct swap_info_struct *si,
 	struct swap_cluster_info *ci;
 	unsigned long tmp, max;
 
Quoted 11 lines

mm/swapfile.c static bool scan_swap_map_try_ssd_cluster(struct swap_info_struct *si, · line 611

Quoted 7 lines
mm/swapfile.c +1 -1
-new_cluster:
 	cluster = this_cpu_ptr(si->percpu_cluster);
 	if (cluster_is_null(&cluster->index)) {
+new_cluster:
 		if (!cluster_list_empty(&si->free_clusters)) {
 			cluster->index = si->free_clusters.head;
 			cluster->next = cluster_next(&cluster->index) *

In swap_do_scheduled_discard(), we will unlock si->lock, so the percpu_cluster may be changed after we releasing the lock. Or the current thread may be moved to a different CPU.

Thanks, you are right.

Quoted 3 lines

Best Regards,
Huang, Ying

Signature · 2 lines
Wei Yang
Help you, Help me
#5 lore raw

Re: [PATCH 3/4] mm/swapfile.c: compare tmp and max after trying to iterate on swap_map

In reply to Wei Yang
Quoted 39 lines · Wei Yang
Quoted 13 lines · Ying
Quoted 11 lines · Wei Yang

There are two duplicate code to handle the case when there is no available swap entry. Just let the code go through and do the check at second place.

No functional change is expected.

---
 mm/swapfile.c | 4 ----
 1 file changed, 4 deletions(-)
Quoted 16 lines

mm/swapfile.c static bool scan_swap_map_try_ssd_cluster(struct swap_info_struct *si, · line 635

Quoted 12 lines
mm/swapfile.c +0 -4
index 3aae700f9931..07b0bc095411 100644
@@ -629,10 +629,6 @@ static bool scan_swap_map_try_ssd_cluster(struct swap_info_struct *si,
 	tmp = cluster->next;
 	max = min_t(unsigned long, si->max,
 		    (cluster_next(&cluster->index) + 1) * SWAPFILE_CLUSTER);
-	if (tmp >= max) {
-		cluster_set_null(&cluster->index);
-		goto new_cluster;
-	}

The code is to avoid to acquire the cluster lock unnecessarily. So I think we should keep this.

If you really want to avoid the lock, my suggestion is to add:

  if (tmp < max) {
      ci = lock_cluster(si, tmp);
          while (tmp < max) {
	  ...
	  }
      unlock_cluster(ci);
Quoted 3 lines

}

Instead of do the similar thing twice.

This is a coding style problem. The original code is common to avoid too many nested code block. But in this case, I think both works.

Best Regards,
Huang, Ying

Quoted 6 lines
Quoted 6 lines

Best Regards,
Huang, Ying

Quoted 3 lines
mm/swapfile.c +0 -0
 	ci = lock_cluster(si, tmp);
 	while (tmp < max) {
 		if (!si->swap_map[tmp])