Skip to contentSkip to conversation

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

PATCH In review Kiryl Shutsemau (Meta) 4 participants 6 messages Nov 16 – Nov 17

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

[...]

#2 lore raw
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
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
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