Skip to contentSkip to conversation

mm: swap: remove duplicate nr_swap_pages decrement in get_swap_page_of_type()

PATCH Applied Youngjun Park 4 participants 6 messages Nov 2, 2025 – Nov 4, 2025

Subsystems (3)
lore
Share & tools
lore

Youngjun Park

author reviewer
#0 lore raw

PATCH v2 1/1 mm: swap: remove duplicate nr_swap_pages decrement in get_swap_page_of_type()

After commit 4f78252da887, nr_swap_pages is decremented in swap_range_alloc(). Since cluster_alloc_swap_entry() calls swap_range_alloc() internally, the decrement in get_swap_page_of_type() causes double-decrementing.

Remove the duplicate decrement.

Fixes 4f78252da887 ("mm: swap: move nr_swap_pages counter decrement from folio_alloc_swap() to swap_range_alloc()")
Cc [email protected] # v6.17-rc1

---
v1 -> v2:
- Collect Acked-by from Chris - thank you!
- Collect Reviewed-by from Barry - thank you!
- Link to v1: https://lore.kernel.org/linux-mm/[email protected]/

 mm/swapfile.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
mm/swapfile.c +1 -3
index 543f303f101d..66a502cd747b 100644
@@ -2020,10 +2020,8 @@ swp_entry_t get_swap_page_of_type(int type)
 			local_lock(&percpu_swap_cluster.lock);
 			offset = cluster_alloc_swap_entry(si, 0, 1);
 			local_unlock(&percpu_swap_cluster.lock);
-			if (offset) {
+			if (offset)
 				entry = swp_entry(si->type, offset);
-				atomic_long_dec(&nr_swap_pages);
-			}
 		}
 		put_swap_device(si);
 	}
Signature · 1 line
2.34.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 39 lines · Youngjun Park

After commit 4f78252da887, nr_swap_pages is decremented in swap_range_alloc(). Since cluster_alloc_swap_entry() calls swap_range_alloc() internally, the decrement in get_swap_page_of_type() causes double-decrementing.

Remove the duplicate decrement.

Fixes 4f78252da887 ("mm: swap: move nr_swap_pages counter decrement from folio_alloc_swap() to swap_range_alloc()")
Cc [email protected] # v6.17-rc1

---
v1 -> v2:
- Collect Acked-by from Chris - thank you!
- Collect Reviewed-by from Barry - thank you!
- Link to v1: https://lore.kernel.org/linux-mm/[email protected]/

 mm/swapfile.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
mm/swapfile.c +1 -4
index 543f303f101d..66a502cd747b 100644
@@ -2020,10 +2020,8 @@ swp_entry_t get_swap_page_of_type(int type)
                        local_lock(&percpu_swap_cluster.lock);
                        offset = cluster_alloc_swap_entry(si, 0, 1);
                        local_unlock(&percpu_swap_cluster.lock);
-                       if (offset) {
+                       if (offset)
                                entry = swp_entry(si->type, offset);
-                               atomic_long_dec(&nr_swap_pages);
-                       }
                }
                put_swap_device(si);
        }
--
Quoted 1 line

2.34.1

Thanks.

maintainer
#2 lore raw
In reply to Youngjun Park
Quoted 6 lines · On Sun, 2 Nov 2025 17:24:56 +0900 Youngjun Park <[email protected]> wrote:

After commit 4f78252da887, nr_swap_pages is decremented in swap_range_alloc(). Since cluster_alloc_swap_entry() calls swap_range_alloc() internally, the decrement in get_swap_page_of_type() causes double-decrementing.

Remove the duplicate decrement.

Can we please have a description of the userspace-visible runtime effects of the bug?

Quoted 2 lines
Fixes 4f78252da887 ("mm: swap: move nr_swap_pages counter decrement from folio_alloc_swap() to swap_range_alloc()")
Cc [email protected] # v6.17-rc1

Especially when proposing a backport.

Thanks.

author reviewer
#3 lore raw
Quoted 9 lines · Andrew Morton
Quoted 6 lines · On Sun, 2 Nov 2025 17:24:56 +0900 Youngjun Park <[email protected]> wrote:

After commit 4f78252da887, nr_swap_pages is decremented in swap_range_alloc(). Since cluster_alloc_swap_entry() calls swap_range_alloc() internally, the decrement in get_swap_page_of_type() causes double-decrementing.

Remove the duplicate decrement.

Quoted 8 lines

Can we please have a description of the userspace-visible runtime effects of the bug?

Quoted 2 lines
Fixes 4f78252da887 ("mm: swap: move nr_swap_pages counter decrement from folio_alloc_swap() to swap_range_alloc()")
Cc [email protected] # v6.17-rc1

Especially when proposing a backport.

Thanks.

Hi Andrew,

Thank you for picking up the patch. Since it's already in mm-hotfixes-unstable, I'm providing the elaboration here rather than sending v3.

As a representative userspace-visible runtime example of the impact, /proc/meminfo reports increasingly inaccurate SwapFree values. The discrepancy grows with each swap allocation, and during hibernation when large amounts of memory are written to swap, the reported value can deviate significantly from actual available swap space, misleading users and monitoring tools.

Best Regards,
Youngjun

maintainer
#4 lore raw
Quoted 14 lines · On Tue, 4 Nov 2025 23:46:40 +0900 YoungJun Park <[email protected]> wrote:
Quoted 8 lines

Can we please have a description of the userspace-visible runtime effects of the bug?

Quoted 2 lines
Fixes 4f78252da887 ("mm: swap: move nr_swap_pages counter decrement from folio_alloc_swap() to swap_range_alloc()")
Cc [email protected] # v6.17-rc1

Especially when proposing a backport.

Thanks.

Hi Andrew,

Thank you for picking up the patch. Since it's already in mm-hotfixes-unstable, I'm providing the elaboration here rather than sending v3.

Quoted 6 lines

As a representative userspace-visible runtime example of the impact, /proc/meminfo reports increasingly inaccurate SwapFree values. The discrepancy grows with each swap allocation, and during hibernation when large amounts of memory are written to swap, the reported value can deviate significantly from actual available swap space, misleading users and monitoring tools.

Great, thanks, very helpful. I pasted that into the mm.git copy of this patch.

reviewer Acked-by on v2 1/1
#5 lore raw
In reply to Youngjun Park
Quoted 6 lines · Youngjun Park

After commit 4f78252da887, nr_swap_pages is decremented in swap_range_alloc(). Since cluster_alloc_swap_entry() calls swap_range_alloc() internally, the decrement in get_swap_page_of_type() causes double-decrementing.

Quoted 7 lines

Remove the duplicate decrement.

Fixes 4f78252da887 ("mm: swap: move nr_swap_pages counter decrement from folio_alloc_swap() to swap_range_alloc()")
Cc [email protected] # v6.17-rc1