Skip to contentSkip to conversation

mm/swapfile: fix and cleanup swap list iterations

PATCH Applied Youngjun Park 6 participants 10 messages Nov 27, 2025 – Feb 22

Subsystems (3)
lore
Share & tools
lore

Youngjun Park

author reviewer
#0 lore raw

v2 cover mm/swapfile: fix and cleanup swap list iterations

This series fixes a potential list iteration issue in swap_sync_discard() when devices are removed, and includes a cleanup for __folio_throttle_swaprate().

v2 -> v1:
- Keep the original return value style. (Thank you Kairui)
- Clarified commit message based on discussion. (Thank you Baoquan He)
- Collect tags.
- Updated only patch [1/2], base commit unchanged. (minor fix only).

Link v1: https://lore.kernel.org/linux-mm/aSfkrpV7T7g5mhpI@yjaykim-PowerEdge-T330/T/#m101dc552904259e26d6595f91d9cbee37b6da7ac

Youngjun Park (2):
mm/swapfile: fix list iteration when next node is removed during
discard
mm/swapfile: use plist_for_each_entry in __folio_throttle_swaprate

 mm/swapfile.c | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)
Signature · 6 lines
base-commit: 624c4d7b8fac3fdf9894a5f89f84709771db4dbb
2.34.1

Best regards,
--
Youngjun Park
author reviewer
#1 lore raw

PATCH v2 1/2 mm/swapfile: fix list iteration when next node is removed during discard

When the next node is removed from the plist (e.g. by swapoff), plist_del() makes the node point to itself, causing the iteration to loop on the same entry indefinitely.

Add a plist_node_empty() check to detect this case and restart iteration, allowing swap_sync_discard() to continue processing remaining swap devices that still have pending discard entries.

Additionally, switch from swap_avail_lock/swap_avail_head to swap_lock/swap_active_head so that iteration is only affected by swapoff operations rather than frequent availability changes, reducing exceptional condition checks and lock contention.

Fixes 686ea517f471 ("mm, swap: do not perform synchronous discard during allocation")
---
 mm/swapfile.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
mm/swapfile.c +9 -5
index d12332423a06..8116f36e440b 100644
@@ -1387,9 +1387,10 @@ static bool swap_sync_discard(void)
 	bool ret = false;
 	struct swap_info_struct *si, *next;
 
-	spin_lock(&swap_avail_lock);
-	plist_for_each_entry_safe(si, next, &swap_avail_head, avail_list) {
-		spin_unlock(&swap_avail_lock);
+	spin_lock(&swap_lock);
+start_over:
+	plist_for_each_entry_safe(si, next, &swap_active_head, list) {
+		spin_unlock(&swap_lock);
 		if (get_swap_device_info(si)) {
 			if (si->flags & SWP_PAGE_DISCARD)
 				ret = swap_do_scheduled_discard(si);
@@ -1397,9 +1398,12 @@ static bool swap_sync_discard(void)
 		}
 		if (ret)
 			return true;
-		spin_lock(&swap_avail_lock);
+
+		spin_lock(&swap_lock);
+		if (plist_node_empty(&next->list))
+			goto start_over;
 	}
-	spin_unlock(&swap_avail_lock);
+	spin_unlock(&swap_lock);
 
 	return false;
 }
Signature · 1 line
2.34.1
Reviewed-by on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] mm/swapfile: fix list iteration when next node is removed during discard

Quoted 13 lines · Youngjun Park

When the next node is removed from the plist (e.g. by swapoff), plist_del() makes the node point to itself, causing the iteration to loop on the same entry indefinitely.

Add a plist_node_empty() check to detect this case and restart iteration, allowing swap_sync_discard() to continue processing remaining swap devices that still have pending discard entries.

Additionally, switch from swap_avail_lock/swap_avail_head to swap_lock/swap_active_head so that iteration is only affected by swapoff operations rather than frequent availability changes, reducing exceptional condition checks and lock contention.

Quoted 7 lines
Fixes 686ea517f471 ("mm, swap: do not perform synchronous discard during allocation")
---
 mm/swapfile.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
Quoted 37 lines
mm/swapfile.c +9 -5
index d12332423a06..8116f36e440b 100644
@@ -1387,9 +1387,10 @@ static bool swap_sync_discard(void)
 	bool ret = false;
 	struct swap_info_struct *si, *next;
 
-	spin_lock(&swap_avail_lock);
-	plist_for_each_entry_safe(si, next, &swap_avail_head, avail_list) {
-		spin_unlock(&swap_avail_lock);
+	spin_lock(&swap_lock);
+start_over:
+	plist_for_each_entry_safe(si, next, &swap_active_head, list) {
+		spin_unlock(&swap_lock);
 		if (get_swap_device_info(si)) {
 			if (si->flags & SWP_PAGE_DISCARD)
 				ret = swap_do_scheduled_discard(si);
@@ -1397,9 +1398,12 @@ static bool swap_sync_discard(void)
 		}
 		if (ret)
 			return true;
-		spin_lock(&swap_avail_lock);
+
+		spin_lock(&swap_lock);
+		if (plist_node_empty(&next->list))
+			goto start_over;
 	}
-	spin_unlock(&swap_avail_lock);
+	spin_unlock(&swap_lock);
 
 	return false;
 }
Signature · 1 line
2.34.1
#3 lore raw

Re: [PATCH v2 1/2] mm/swapfile: fix list iteration when next node is removed during discard

In reply to Youngjun Park
Quoted 13 lines · On Thu, 27 Nov 2025 19:03:02 +0900 Youngjun Park <[email protected]> wrote:

When the next node is removed from the plist (e.g. by swapoff), plist_del() makes the node point to itself, causing the iteration to loop on the same entry indefinitely.

Add a plist_node_empty() check to detect this case and restart iteration, allowing swap_sync_discard() to continue processing remaining swap devices that still have pending discard entries.

Additionally, switch from swap_avail_lock/swap_avail_head to swap_lock/swap_active_head so that iteration is only affected by swapoff operations rather than frequent availability changes, reducing exceptional condition checks and lock contention.

Quoted 5 lines
Fixes 686ea517f471 ("mm, swap: do not perform synchronous discard during allocation")

Hi everyone,

This fix landed upstream in v6.19-rc1:

commit f9e82f99b3771eef396dbf97e0f3c76e20af60dd
Author: Youngjun Park <[email protected]>
Date: Thu Nov 27 19:03:02 2025 +0900
Subject: mm/swapfile: fix list iteration when next node is removed during discard

Looks like the commit being fixed is actually:

commit 9fb749cd15078c7bdc46e5d45c37493f83323e33
Author: Kairui Song <[email protected]>
Date: Fri Oct 24 02:34:11 2025 +0800
Subject: mm, swap: do not perform synchronous discard during allocation

v6.18.y stable has Kairui Song's commit, but this fix hasn't made it to stable. Johannes noticed the Fixes: tag doesn't match, which probably explains the gap, but I think we should pull this fix (f9e82f99b) in.

Quoted 42 lines
---
 mm/swapfile.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
mm/swapfile.c +9 -5
index d12332423a06..8116f36e440b 100644
@@ -1387,9 +1387,10 @@ static bool swap_sync_discard(void)
 	bool ret = false;
 	struct swap_info_struct *si, *next;
 
-	spin_lock(&swap_avail_lock);
-	plist_for_each_entry_safe(si, next, &swap_avail_head, avail_list) {
-		spin_unlock(&swap_avail_lock);
+	spin_lock(&swap_lock);
+start_over:
+	plist_for_each_entry_safe(si, next, &swap_active_head, list) {
+		spin_unlock(&swap_lock);
 		if (get_swap_device_info(si)) {
 			if (si->flags & SWP_PAGE_DISCARD)
 				ret = swap_do_scheduled_discard(si);
@@ -1397,9 +1398,12 @@ static bool swap_sync_discard(void)
 		}
 		if (ret)
 			return true;
-		spin_lock(&swap_avail_lock);
+
+		spin_lock(&swap_lock);
+		if (plist_node_empty(&next->list))
+			goto start_over;
 	}
-	spin_unlock(&swap_avail_lock);
+	spin_unlock(&swap_lock);
 
 	return false;
 }
Signature · 1 line
2.34.1
author reviewer Acked-by
#4 lore raw

Re: [PATCH v2 1/2] mm/swapfile: fix list iteration when next node is removed during discard

Quoted 32 lines · Chris Mason
Quoted 13 lines · On Thu, 27 Nov 2025 19:03:02 +0900 Youngjun Park <[email protected]> wrote:

When the next node is removed from the plist (e.g. by swapoff), plist_del() makes the node point to itself, causing the iteration to loop on the same entry indefinitely.

Add a plist_node_empty() check to detect this case and restart iteration, allowing swap_sync_discard() to continue processing remaining swap devices that still have pending discard entries.

Additionally, switch from swap_avail_lock/swap_avail_head to swap_lock/swap_active_head so that iteration is only affected by swapoff operations rather than frequent availability changes, reducing exceptional condition checks and lock contention.

Quoted 5 lines
Fixes 686ea517f471 ("mm, swap: do not perform synchronous discard during allocation")

Hi everyone,

This fix landed upstream in v6.19-rc1:

commit f9e82f99b3771eef396dbf97e0f3c76e20af60dd
Author: Youngjun Park <[email protected]>
Date: Thu Nov 27 19:03:02 2025 +0900
Subject: mm/swapfile: fix list iteration when next node is removed during discard

Looks like the commit being fixed is actually:

Quoted 8 lines

commit 9fb749cd15078c7bdc46e5d45c37493f83323e33
Author: Kairui Song <[email protected]>
Date: Fri Oct 24 02:34:11 2025 +0800
Subject: mm, swap: do not perform synchronous discard during allocation

v6.18.y stable has Kairui Song's commit, but this fix hasn't made it to stable. Johannes noticed the Fixes: tag doesn't match, which probably explains the gap, but I think we should pull this fix (f9e82f99b) in.

+Cc: [email protected]
Thanks for catching this and pushing for the stable backport.

maintainer
#5 lore raw

Re: [PATCH v2 1/2] mm/swapfile: fix list iteration when next node is removed during discard

Quoted 6 lines · On Sun, 22 Feb 2026 10:41:50 +0900 YoungJun Park <[email protected]> wrote:
Quoted 5 lines

Hi everyone,

This fix landed upstream in v6.19-rc1:

commit f9e82f99b3771eef396dbf97e0f3c76e20af60dd

Quoted 7 lines
Quoted 7 lines

Author: Youngjun Park <[email protected]>
Date: Thu Nov 27 19:03:02 2025 +0900
Subject: mm/swapfile: fix list iteration when next node is removed during discard

Looks like the commit being fixed is actually:

commit 9fb749cd15078c7bdc46e5d45c37493f83323e33

yup. I've updated the changelog.

author reviewer
#6 lore raw

PATCH v2 2/2 mm/swapfile: use plist_for_each_entry in __folio_throttle_swaprate

In reply to Youngjun Park

The loop breaks immediately after finding the first swap device and never modifies the list. Replace plist_for_each_entry_safe() with plist_for_each_entry() and remove the unused next variable.

---
 mm/swapfile.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
mm/swapfile.c +2 -3
index 8116f36e440b..46d2008e4b99 100644
@@ -4023,7 +4023,7 @@ static bool __has_usable_swap(void)
 
 void __folio_throttle_swaprate(struct folio *folio, gfp_t gfp)
 {
-	struct swap_info_struct *si, *next;
+	struct swap_info_struct *si;
 
 	if (!(gfp & __GFP_IO))
 		return;
@@ -4042,8 +4042,7 @@ void __folio_throttle_swaprate(struct folio *folio, gfp_t gfp)
 		return;
 
 	spin_lock(&swap_avail_lock);
-	plist_for_each_entry_safe(si, next, &swap_avail_head,
-				  avail_list) {
+	plist_for_each_entry(si, &swap_avail_head, avail_list) {
 		if (si->bdev) {
 			blkcg_schedule_throttle(si->bdev->bd_disk, true);
 			break;
Signature · 1 line
2.34.1
Reviewed-by on v2 2/2
#7 lore raw

Re: [PATCH v2 2/2] mm/swapfile: use plist_for_each_entry in __folio_throttle_swaprate

Quoted 3 lines · Youngjun Park

The loop breaks immediately after finding the first swap device and never modifies the list. Replace plist_for_each_entry_safe() with plist_for_each_entry() and remove the unused next variable.

Yeah, and the lock is always held. So this looks good to me.

Quoted 32 lines
---
 mm/swapfile.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
mm/swapfile.c +2 -3
index 8116f36e440b..46d2008e4b99 100644
@@ -4023,7 +4023,7 @@ static bool __has_usable_swap(void)
 
 void __folio_throttle_swaprate(struct folio *folio, gfp_t gfp)
 {
-	struct swap_info_struct *si, *next;
+	struct swap_info_struct *si;
 
 	if (!(gfp & __GFP_IO))
 		return;
@@ -4042,8 +4042,7 @@ void __folio_throttle_swaprate(struct folio *folio, gfp_t gfp)
 		return;
 
 	spin_lock(&swap_avail_lock);
-	plist_for_each_entry_safe(si, next, &swap_avail_head,
-				  avail_list) {
+	plist_for_each_entry(si, &swap_avail_head, avail_list) {
 		if (si->bdev) {
 			blkcg_schedule_throttle(si->bdev->bd_disk, true);
 			break;
Signature · 1 line
2.34.1
Acked-by on v2 2/2
#8 lore raw

Re: [PATCH v2 2/2] mm/swapfile: use plist_for_each_entry in __folio_throttle_swaprate

In reply to Youngjun Park
Quoted 34 lines · Youngjun Park

The loop breaks immediately after finding the first swap device and never modifies the list. Replace plist_for_each_entry_safe() with plist_for_each_entry() and remove the unused next variable.

---
 mm/swapfile.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
mm/swapfile.c +2 -4
index 8116f36e440b..46d2008e4b99 100644
@@ -4023,7 +4023,7 @@ static bool __has_usable_swap(void)
 
 void __folio_throttle_swaprate(struct folio *folio, gfp_t gfp)
 {
-       struct swap_info_struct *si, *next;
+       struct swap_info_struct *si;
 
        if (!(gfp & __GFP_IO))
                return;
@@ -4042,8 +4042,7 @@ void __folio_throttle_swaprate(struct folio *folio, gfp_t gfp)
                return;
 
        spin_lock(&swap_avail_lock);
-       plist_for_each_entry_safe(si, next, &swap_avail_head,
-                                 avail_list) {
+       plist_for_each_entry(si, &swap_avail_head, avail_list) {
                if (si->bdev) {
                        blkcg_schedule_throttle(si->bdev->bd_disk, true);
                        break;
--
Quoted 2 lines

2.34.1

Thanks!

maintainer Acked-by on v2 2/2
#9 lore raw

Re: [PATCH v2 2/2] mm/swapfile: use plist_for_each_entry in __folio_throttle_swaprate

In reply to Youngjun Park

Hi Youngjun,

Thanks for the patch!

Chris

Quoted 36 lines · Youngjun Park

The loop breaks immediately after finding the first swap device and never modifies the list. Replace plist_for_each_entry_safe() with plist_for_each_entry() and remove the unused next variable.

---
 mm/swapfile.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
mm/swapfile.c +2 -4
index 8116f36e440b..46d2008e4b99 100644
@@ -4023,7 +4023,7 @@ static bool __has_usable_swap(void)
 
 void __folio_throttle_swaprate(struct folio *folio, gfp_t gfp)
 {
-       struct swap_info_struct *si, *next;
+       struct swap_info_struct *si;
 
        if (!(gfp & __GFP_IO))
                return;
@@ -4042,8 +4042,7 @@ void __folio_throttle_swaprate(struct folio *folio, gfp_t gfp)
                return;
 
        spin_lock(&swap_avail_lock);
-       plist_for_each_entry_safe(si, next, &swap_avail_head,
-                                 avail_list) {
+       plist_for_each_entry(si, &swap_avail_head, avail_list) {
                if (si->bdev) {
                        blkcg_schedule_throttle(si->bdev->bd_disk, true);
                        break;
--

2.34.1