Skip to contentSkip to conversation

mm/compaction: do not skip the free scanner's periodic lock drop

PATCH In review Kiryl Shutsemau (Meta) 5 participants 7 messages Nov 16 – Dec 2

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm/compaction: do not skip the free scanner's periodic lock drop

isolate_freepages_block() drops zone->lock, checks for a fatal signal and calls cond_resched() at every pfn that is a multiple of COMPACT_CLUSTER_MAX.

With a stride above one, the scan steps over a compound page, or over a high-order free page it has just isolated, to a pfn that is not a multiple of the stride. From there it never reaches a multiple of COMPACT_CLUSTER_MAX, so the rest of the pageblock is scanned without the check, possibly with zone->lock held and interrupts off.

Count probes instead, and check every COMPACT_CLUSTER_MAX of them.

Fixes 4fca9730c51d ("mm, compaction: sample pageblocks for free pages")
---
 mm/compaction.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
mm/compaction.c +7 -5
index f1b2060eb201..4465c1551406 100644
@@ -579,7 +579,7 @@ static unsigned long isolate_freepages_block(struct compact_control *cc,
 				unsigned int stride,
 				bool strict)
 {
-	int nr_scanned = 0, total_isolated = 0;
+	int nr_scanned = 0, nr_probes = 0, total_isolated = 0;
 	struct page *page;
 	unsigned long flags = 0;
 	bool locked = false;
@@ -599,13 +599,15 @@ static unsigned long isolate_freepages_block(struct compact_control *cc,
 		/*
 		 * Periodically drop the lock (if held) regardless of its
 		 * contention, to give chance to IRQs. Abort if fatal signal
-		 * pending.
+		 * pending. Count probes rather than pfns, as the scan does not
+		 * always land on a multiple of COMPACT_CLUSTER_MAX.
 		 */
-		if (!(blockpfn % COMPACT_CLUSTER_MAX)
-		    && compact_unlock_should_abort(&cc->zone->lock, flags,
-								&locked, cc))
+		if (!(nr_probes % COMPACT_CLUSTER_MAX) &&
+		    compact_unlock_should_abort(&cc->zone->lock, flags,
+						&locked, cc))
 			break;
 
+		nr_probes++;
 		nr_scanned++;
 
 		/*

base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4

Signature · 1 line
2.54.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · On Mon, 16 Nov 2026 12:57:45 +0000 "Kiryl Shutsemau (Meta)" <[email protected]>…

isolate_freepages_block() drops zone->lock, checks for a fatal signal and calls cond_resched() at every pfn that is a multiple of COMPACT_CLUSTER_MAX.

With a stride above one, the scan steps over a compound page, or over a high-order free page it has just isolated, to a pfn that is not a multiple of the stride. From there it never reaches a multiple of COMPACT_CLUSTER_MAX, so the rest of the pageblock is scanned without the check, possibly with zone->lock held and interrupts off.

Count probes instead, and check every COMPACT_CLUSTER_MAX of them.

Makes sense to me.

Quoted 2 lines
Fixes 4fca9730c51d ("mm, compaction: sample pageblocks for free pages")

I understand you intentionally not Cc-ing stable@, buecase the user impact is not that bad. I think that makes sense. I just wanted to call out this for a case that it was not intentional.

Quoted 22 lines
---
 mm/compaction.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
mm/compaction.c +1 -1
index f1b2060eb201..4465c1551406 100644
@@ -579,7 +579,7 @@ static unsigned long isolate_freepages_block(struct compact_control *cc,
 				unsigned int stride,
 				bool strict)
 {
-	int nr_scanned = 0, total_isolated = 0;
+	int nr_scanned = 0, nr_probes = 0, total_isolated = 0;
 	struct page *page;
 	unsigned long flags = 0;
 	bool locked = false;
@@ -599,13 +599,15 @@ static unsigned long isolate_freepages_block(struct compact_control *cc,
 		/*
 		 * Periodically drop the lock (if held) regardless of its
 		 * contention, to give chance to IRQs. Abort if fatal signal

mm/compaction.c static unsigned long isolate_freepages_block(struct compact_control *cc, · line 607

Quoted 10 lines
mm/compaction.c +5 -4
-		 * pending.
+		 * pending. Count probes rather than pfns, as the scan does not
+		 * always land on a multiple of COMPACT_CLUSTER_MAX.
 		 */
-		if (!(blockpfn % COMPACT_CLUSTER_MAX)
-		    && compact_unlock_should_abort(&cc->zone->lock, flags,
-								&locked, cc))
+		if (!(nr_probes % COMPACT_CLUSTER_MAX) &&
+		    compact_unlock_should_abort(&cc->zone->lock, flags,
+						&locked, cc))

Why don't you let the later two lines unchanged? No strong opinion, no blocker.

Thanks,
SJ

[...]

Reviewed-by on v1 1/1
#3 lore raw
In reply to Kiryl Shutsemau (Meta)
Quoted 10 lines · Kiryl Shutsemau (Meta)

isolate_freepages_block() drops zone->lock, checks for a fatal signal and calls cond_resched() at every pfn that is a multiple of COMPACT_CLUSTER_MAX.

With a stride above one, the scan steps over a compound page, or over a high-order free page it has just isolated, to a pfn that is not a multiple of the stride. From there it never reaches a multiple of COMPACT_CLUSTER_MAX, so the rest of the pageblock is scanned without the check, possibly with zone->lock held and interrupts off.

Quoted 5 lines

Count probes instead, and check every COMPACT_CLUSTER_MAX of them.

Fixes 4fca9730c51d ("mm, compaction: sample pageblocks for free pages")

---

Cc stable?

Quoted 20 lines
 mm/compaction.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
mm/compaction.c +1 -1
index f1b2060eb201..4465c1551406 100644
@@ -579,7 +579,7 @@ static unsigned long isolate_freepages_block(struct compact_control *cc,
 				unsigned int stride,
 				bool strict)
 {
-	int nr_scanned = 0, total_isolated = 0;
+	int nr_scanned = 0, nr_probes = 0, total_isolated = 0;
 	struct page *page;
 	unsigned long flags = 0;
 	bool locked = false;
@@ -599,13 +599,15 @@ static unsigned long isolate_freepages_block(struct compact_control *cc,
 		/*
 		 * Periodically drop the lock (if held) regardless of its
 		 * contention, to give chance to IRQs. Abort if fatal signal

mm/compaction.c static unsigned long isolate_freepages_block(struct compact_control *cc, · line 607

Quoted 10 lines
mm/compaction.c +5 -4
-		 * pending.
+		 * pending. Count probes rather than pfns, as the scan does not
+		 * always land on a multiple of COMPACT_CLUSTER_MAX.
 		 */
-		if (!(blockpfn % COMPACT_CLUSTER_MAX)
-		    && compact_unlock_should_abort(&cc->zone->lock, flags,
-								&locked, cc))
+		if (!(nr_probes % COMPACT_CLUSTER_MAX) &&
+		    compact_unlock_should_abort(&cc->zone->lock, flags,
+						&locked, cc))

I see this is an unrelated change but I don't mind.

Quoted 8 lines
mm/compaction.c +1 -0
 			break;
 
+		nr_probes++;
 		nr_scanned++;
 
 		/*

base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4

#4 lore raw
Quoted 13 lines · Dev Jain
Quoted 10 lines · Kiryl Shutsemau (Meta)

isolate_freepages_block() drops zone->lock, checks for a fatal signal and calls cond_resched() at every pfn that is a multiple of COMPACT_CLUSTER_MAX.

With a stride above one, the scan steps over a compound page, or over a high-order free page it has just isolated, to a pfn that is not a multiple of the stride. From there it never reaches a multiple of COMPACT_CLUSTER_MAX, so the rest of the pageblock is scanned without the check, possibly with zone->lock held and interrupts off.

Quoted 7 lines
Quoted 5 lines

Count probes instead, and check every COMPACT_CLUSTER_MAX of them.

Fixes 4fca9730c51d ("mm, compaction: sample pageblocks for free pages")

---

Cc stable?

I have not seen real world problem triggered by the bug. I don't think it is needed.

Quoted 2 lines

Thanks!

Quoted 24 lines
Quoted 24 lines
 mm/compaction.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
mm/compaction.c +3 -2
index f1b2060eb201..4465c1551406 100644
@@ -579,7 +579,7 @@ static unsigned long isolate_freepages_block(struct compact_control *cc,
 				unsigned int stride,
 				bool strict)
 {
-	int nr_scanned = 0, total_isolated = 0;
+	int nr_scanned = 0, nr_probes = 0, total_isolated = 0;
 	struct page *page;
 	unsigned long flags = 0;
 	bool locked = false;
@@ -599,13 +599,15 @@ static unsigned long isolate_freepages_block(struct compact_control *cc,
 		/*
 		 * Periodically drop the lock (if held) regardless of its
 		 * contention, to give chance to IRQs. Abort if fatal signal
-		 * pending.
+		 * pending. Count probes rather than pfns, as the scan does not
+		 * always land on a multiple of COMPACT_CLUSTER_MAX.
 		 */
Quoted 8 lines

mm/compaction.c static unsigned long isolate_freepages_block(struct compact_control *cc, · line 607

Quoted 6 lines
mm/compaction.c +3 -3
-		if (!(blockpfn % COMPACT_CLUSTER_MAX)
-		    && compact_unlock_should_abort(&cc->zone->lock, flags,
-								&locked, cc))
+		if (!(nr_probes % COMPACT_CLUSTER_MAX) &&
+		    compact_unlock_should_abort(&cc->zone->lock, flags,
+						&locked, cc))

I see this is an unrelated change but I don't mind.

&& on the new line looks odd to me, it belongs to the first line.

Signature · 1 line
  Kiryl Shutsemau / Kirill A. Shutemov
#2 lore raw
In reply to SJ Park
Quoted 14 lines · SJ Park
Quoted 11 lines · On Mon, 16 Nov 2026 12:57:45 +0000 "Kiryl Shutsemau (Meta)" <[email protected]>…

isolate_freepages_block() drops zone->lock, checks for a fatal signal and calls cond_resched() at every pfn that is a multiple of COMPACT_CLUSTER_MAX.

With a stride above one, the scan steps over a compound page, or over a high-order free page it has just isolated, to a pfn that is not a multiple of the stride. From there it never reaches a multiple of COMPACT_CLUSTER_MAX, so the rest of the pageblock is scanned without the check, possibly with zone->lock held and interrupts off.

Count probes instead, and check every COMPACT_CLUSTER_MAX of them.

Quoted 8 lines

Makes sense to me.

Quoted 2 lines
Fixes 4fca9730c51d ("mm, compaction: sample pageblocks for free pages")

I understand you intentionally not Cc-ing stable@, buecase the user impact is not that bad. I think that makes sense. I just wanted to call out this for a case that it was not intentional.

Right, I have not see the actual user effect of the bug.

Quoted 27 lines
Quoted 27 lines
---
 mm/compaction.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
mm/compaction.c +3 -3
index f1b2060eb201..4465c1551406 100644
@@ -579,7 +579,7 @@ static unsigned long isolate_freepages_block(struct compact_control *cc,
 				unsigned int stride,
 				bool strict)
 {
-	int nr_scanned = 0, total_isolated = 0;
+	int nr_scanned = 0, nr_probes = 0, total_isolated = 0;
 	struct page *page;
 	unsigned long flags = 0;
 	bool locked = false;
@@ -599,13 +599,15 @@ static unsigned long isolate_freepages_block(struct compact_control *cc,
 		/*
 		 * Periodically drop the lock (if held) regardless of its
 		 * contention, to give chance to IRQs. Abort if fatal signal
-		 * pending.
+		 * pending. Count probes rather than pfns, as the scan does not
+		 * always land on a multiple of COMPACT_CLUSTER_MAX.
 		 */
-		if (!(blockpfn % COMPACT_CLUSTER_MAX)
Quoted 8 lines

mm/compaction.c static unsigned long isolate_freepages_block(struct compact_control *cc, · line 607

Quoted 5 lines
mm/compaction.c +3 -2
-		    && compact_unlock_should_abort(&cc->zone->lock, flags,
-								&locked, cc))
+		if (!(nr_probes % COMPACT_CLUSTER_MAX) &&
+		    compact_unlock_should_abort(&cc->zone->lock, flags,
+						&locked, cc))

Why don't you let the later two lines unchanged? No strong opinion, no blocker.

&& on the new line is odd to me.

Quoted 1 line

Thanks!

Signature · 1 line
  Kiryl Shutsemau / Kirill A. Shutemov
reviewer Reviewed-by on v1 1/1
#5 lore raw
In reply to Kiryl Shutsemau (Meta)
Quoted 11 lines · Kiryl Shutsemau (Meta)

isolate_freepages_block() drops zone->lock, checks for a fatal signal and calls cond_resched() at every pfn that is a multiple of COMPACT_CLUSTER_MAX.

With a stride above one, the scan steps over a compound page, or over a high-order free page it has just isolated, to a pfn that is not a multiple of the stride. From there it never reaches a multiple of COMPACT_CLUSTER_MAX, so the rest of the pageblock is scanned without the check, possibly with zone->lock held and interrupts off.

Count probes instead, and check every COMPACT_CLUSTER_MAX of them.

Makes sense. The loop is working on one page (regardless of its order) at a time.

Quoted 7 lines
Fixes 4fca9730c51d ("mm, compaction: sample pageblocks for free pages")
---
 mm/compaction.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

Best Regards,
Yan, Zi

maintainer tree notice
#6 lore raw

+ mm-compaction-do-not-skip-the-free-scanners-periodic-lock-drop.patch added to mm-new branch

In reply to Kiryl Shutsemau (Meta)
The patch titled Subject: mm/compaction: do not skip the free scanner's periodic lock drop has been added to the -mm mm-new branch. Its filename is…

The patch titled
Subject: mm/compaction: do not skip the free scanner's periodic lock drop
has been added to the -mm mm-new branch. Its filename is
mm-compaction-do-not-skip-the-free-scanners-periodic-lock-drop.patch

This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-compaction-do-not-skip-the-free-scanners-periodic-lock-drop.patch

This patch will later appear in the mm-new branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

Note, mm-new is a provisional staging ground for work-in-progress patches, and acceptance into mm-new is a notification for others take notice and to finish up reviews. Please do not hesitate to respond to review feedback and post updated versions to replace or incrementally fixup patches in mm-new.

The mm-new branch of mm.git is not included in linux-next

If a few days of testing in mm-new is successful, the patch will me moved into mm.git's mm-unstable branch, which is included in linux-next

Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's

*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***

The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days

------------------------------------------------------
From: "Kiryl Shutsemau (Meta)" <[email protected]>
Subject: mm/compaction: do not skip the free scanner's periodic lock drop
Date: Mon, 16 Nov 2026 12:57:45 +0000

isolate_freepages_block() drops zone->lock, checks for a fatal signal and calls cond_resched() at every pfn that is a multiple of COMPACT_CLUSTER_MAX.

With a stride above one, the scan steps over a compound page, or over a high-order free page it has just isolated, to a pfn that is not a multiple of the stride. From there it never reaches a multiple of COMPACT_CLUSTER_MAX, so the rest of the pageblock is scanned without the check, possibly with zone->lock held and interrupts off.

Count probes instead, and check every COMPACT_CLUSTER_MAX of them.

Fixes 4fca9730c51d ("mm, compaction: sample pageblocks for free pages")
Cc Vlastimil Babka <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Brendan Jackman <[email protected]>
 mm/compaction.c |   12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
mm/compaction.c renamed from mm/compaction.c~mm-compaction-do-not-skip-the-free-scanners-periodic-lock-drop +7 -5
@@ -579,7 +579,7 @@ static unsigned long isolate_freepages_b
 				unsigned int stride,
 				bool strict)
 {
-	int nr_scanned = 0, total_isolated = 0;
+	int nr_scanned = 0, nr_probes = 0, total_isolated = 0;
 	struct page *page;
 	unsigned long flags = 0;
 	bool locked = false;
@@ -599,13 +599,15 @@ static unsigned long isolate_freepages_b
 		/*
 		 * Periodically drop the lock (if held) regardless of its
 		 * contention, to give chance to IRQs. Abort if fatal signal
-		 * pending.
+		 * pending. Count probes rather than pfns, as the scan does not
+		 * always land on a multiple of COMPACT_CLUSTER_MAX.
 		 */
-		if (!(blockpfn % COMPACT_CLUSTER_MAX)
-		    && compact_unlock_should_abort(&cc->zone->lock, flags,
-								&locked, cc))
+		if (!(nr_probes % COMPACT_CLUSTER_MAX) &&
+		    compact_unlock_should_abort(&cc->zone->lock, flags,
+						&locked, cc))
 			break;
 
+		nr_probes++;
 		nr_scanned++;
 
 		/*

_

Patches currently in -mm which might be from [email protected] are

mm-compaction-do-not-skip-the-free-scanners-periodic-lock-drop.patch