Skip to contentSkip to conversation

mm: use spinlock guards for zone lock

PATCH RESEND Superseded Dmitry Ilvokhin 3 participants 12 messages Apr 8 – Apr 9

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover mm: use spinlock guards for zone lock

Resending v2 to get feedback from folks who work with this code, as Andrew suggested.

This series uses spinlock guard for zone lock across several mm functions to replace explicit lock/unlock patterns with automatic scope-based cleanup.

This simplifies the control flow by removing 'flags' variables, goto labels, and redundant unlock calls.

Patches are ordered by decreasing value. The first six patches simplify the control flow by removing gotos, multiple unlock paths, or 'ret' variables. The last two are simpler lock/unlock pair conversions that only remove 'flags' and can be dropped if considered unnecessary churn.

Based on mm-stable.

v1 -> v2:

- Andrew Morton raised concerns about binary size increase in v1. Peter Zijlstra has since fixed the underlying issue in the guards infrastructure in tip [1]. Note: the fix is not yet in mm-stable, so it needs to be applied first to reproduce these results. With that fix, bloat-o-meter on x86 defconfig shows a net decrease of 49 bytes (-0.12%) for page_alloc.o.
- Rebased on mm-stable, since the patch this series depended on was dropped from mm-new.
- Converted guard(zone_lock_irqsave)(zone) to guard(spinlock_irqsave)(&zone->lock).
- Dropped redundant braces in unreserve_highatomic_pageblock() (Steven Rostedt)

v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/

[1]: https://lore.kernel.org/all/[email protected]/

Dmitry Ilvokhin (8):
mm: use zone lock guard in reserve_highatomic_pageblock()
mm: use zone lock guard in unset_migratetype_isolate()
mm: use zone lock guard in unreserve_highatomic_pageblock()
mm: use zone lock guard in set_migratetype_isolate()
mm: use zone lock guard in take_page_off_buddy()
mm: use zone lock guard in put_page_back_buddy()
mm: use zone lock guard in free_pcppages_bulk()
mm: use zone lock guard in __offline_isolated_pages()

 mm/page_alloc.c     | 53 ++++++++++++-----------------------
 mm/page_isolation.c | 67 +++++++++++++++++++--------------------------
 2 files changed, 45 insertions(+), 75 deletions(-)
Signature · 1 line
2.52.0
#1 lore raw

PATCH RESEND v2 1/8 mm: use zone lock guard in reserve_highatomic_pageblock()

Use the spinlock_irqsave zone lock guard in reserve_highatomic_pageblock() to replace the explicit lock/unlock and goto out_unlock pattern with automatic scope-based cleanup.

---
 mm/page_alloc.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)
mm/page_alloc.c +5 -8
index 111b54df8a3c..3a4523c35fb6 100644
@@ -3431,7 +3431,7 @@ static void reserve_highatomic_pageblock(struct page *page, int order,
 					 struct zone *zone)
 {
 	int mt;
-	unsigned long max_managed, flags;
+	unsigned long max_managed;
 
 	/*
 	 * The number reserved as: minimum is 1 pageblock, maximum is
@@ -3445,29 +3445,26 @@ static void reserve_highatomic_pageblock(struct page *page, int order,
 	if (zone->nr_reserved_highatomic >= max_managed)
 		return;
 
-	spin_lock_irqsave(&zone->lock, flags);
+	guard(spinlock_irqsave)(&zone->lock);
 
 	/* Recheck the nr_reserved_highatomic limit under the lock */
 	if (zone->nr_reserved_highatomic >= max_managed)
-		goto out_unlock;
+		return;
 
 	/* Yoink! */
 	mt = get_pageblock_migratetype(page);
 	/* Only reserve normal pageblocks (i.e., they can merge with others) */
 	if (!migratetype_is_mergeable(mt))
-		goto out_unlock;
+		return;
 
 	if (order < pageblock_order) {
 		if (move_freepages_block(zone, page, mt, MIGRATE_HIGHATOMIC) == -1)
-			goto out_unlock;
+			return;
 		zone->nr_reserved_highatomic += pageblock_nr_pages;
 	} else {
 		change_pageblock_range(page, order, MIGRATE_HIGHATOMIC);
 		zone->nr_reserved_highatomic += 1 << order;
 	}
-
-out_unlock:
-	spin_unlock_irqrestore(&zone->lock, flags);
 }
 
 /*
Signature · 1 line
2.52.0
#2 lore raw

PATCH RESEND v2 2/8 mm: use zone lock guard in unset_migratetype_isolate()

In reply to Dmitry Ilvokhin

Use spinlock_irqsave zone lock guard in unset_migratetype_isolate() to replace the explicit lock/unlock and goto pattern with automatic scope-based cleanup.

---
 mm/page_isolation.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
mm/page_isolation.c +2 -5
index c48ff5c00244..9d606052dd80 100644
@@ -223,15 +223,14 @@ static int set_migratetype_isolate(struct page *page, enum pb_isolate_mode mode,
 static void unset_migratetype_isolate(struct page *page)
 {
 	struct zone *zone;
-	unsigned long flags;
 	bool isolated_page = false;
 	unsigned int order;
 	struct page *buddy;
 
 	zone = page_zone(page);
-	spin_lock_irqsave(&zone->lock, flags);
+	guard(spinlock_irqsave)(&zone->lock);
 	if (!is_migrate_isolate_page(page))
-		goto out;
+		return;
 
 	/*
 	 * Because freepage with more than pageblock_order on isolated
@@ -279,8 +278,6 @@ static void unset_migratetype_isolate(struct page *page)
 		__putback_isolated_page(page, order, get_pageblock_migratetype(page));
 	}
 	zone->nr_isolate_pageblock--;
-out:
-	spin_unlock_irqrestore(&zone->lock, flags);
 }
 
 static inline struct page *
Signature · 1 line
2.52.0
#3 lore raw

PATCH RESEND v2 3/8 mm: use zone lock guard in unreserve_highatomic_pageblock()

In reply to Dmitry Ilvokhin

Use spinlock_irqsave zone lock guard in unreserve_highatomic_pageblock() to replace the explicit lock/unlock pattern with automatic scope-based cleanup.

---
 mm/page_alloc.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
mm/page_alloc.c +2 -6
index 3a4523c35fb6..0b9a423cbd24 100644
@@ -3480,7 +3480,6 @@ static bool unreserve_highatomic_pageblock(const struct alloc_context *ac,
 						bool force)
 {
 	struct zonelist *zonelist = ac->zonelist;
-	unsigned long flags;
 	struct zoneref *z;
 	struct zone *zone;
 	struct page *page;
@@ -3497,7 +3496,7 @@ static bool unreserve_highatomic_pageblock(const struct alloc_context *ac,
 					pageblock_nr_pages)
 			continue;
 
-		spin_lock_irqsave(&zone->lock, flags);
+		guard(spinlock_irqsave)(&zone->lock);
 		for (order = 0; order < NR_PAGE_ORDERS; order++) {
 			struct free_area *area = &(zone->free_area[order]);
 			unsigned long size;
@@ -3544,12 +3543,9 @@ static bool unreserve_highatomic_pageblock(const struct alloc_context *ac,
 			 * so this should not fail on zone boundaries.
 			 */
 			WARN_ON_ONCE(ret == -1);
-			if (ret > 0) {
-				spin_unlock_irqrestore(&zone->lock, flags);
+			if (ret > 0)
 				return ret;
-			}
 		}
-		spin_unlock_irqrestore(&zone->lock, flags);
 	}
 
 	return false;
Signature · 1 line
2.52.0
#4 lore raw

PATCH RESEND v2 4/8 mm: use zone lock guard in set_migratetype_isolate()

In reply to Dmitry Ilvokhin

Use spinlock_irqsave scoped lock guard in set_migratetype_isolate() to replace the explicit lock/unlock pattern with automatic scope-based cleanup. The scoped variant is used to keep dump_page() outside the locked section to avoid a lockdep splat.

---
 mm/page_isolation.c | 60 ++++++++++++++++++++-------------------------
 1 file changed, 26 insertions(+), 34 deletions(-)
mm/page_isolation.c +26 -34
index 9d606052dd80..7a9d631945a3 100644
@@ -167,48 +167,40 @@ static int set_migratetype_isolate(struct page *page, enum pb_isolate_mode mode,
 {
 	struct zone *zone = page_zone(page);
 	struct page *unmovable;
-	unsigned long flags;
 	unsigned long check_unmovable_start, check_unmovable_end;
 
 	if (PageUnaccepted(page))
 		accept_page(page);
 
-	spin_lock_irqsave(&zone->lock, flags);
-
-	/*
-	 * We assume the caller intended to SET migrate type to isolate.
-	 * If it is already set, then someone else must have raced and
-	 * set it before us.
-	 */
-	if (is_migrate_isolate_page(page)) {
-		spin_unlock_irqrestore(&zone->lock, flags);
-		return -EBUSY;
-	}
-
-	/*
-	 * FIXME: Now, memory hotplug doesn't call shrink_slab() by itself.
-	 * We just check MOVABLE pages.
-	 *
-	 * Pass the intersection of [start_pfn, end_pfn) and the page's pageblock
-	 * to avoid redundant checks.
-	 */
-	check_unmovable_start = max(page_to_pfn(page), start_pfn);
-	check_unmovable_end = min(pageblock_end_pfn(page_to_pfn(page)),
-				  end_pfn);
-
-	unmovable = has_unmovable_pages(check_unmovable_start, check_unmovable_end,
-			mode);
-	if (!unmovable) {
-		if (!pageblock_isolate_and_move_free_pages(zone, page)) {
-			spin_unlock_irqrestore(&zone->lock, flags);
+	scoped_guard(spinlock_irqsave, &zone->lock) {
+		/*
+		 * We assume the caller intended to SET migrate type to
+		 * isolate. If it is already set, then someone else must have
+		 * raced and set it before us.
+		 */
+		if (is_migrate_isolate_page(page))
 			return -EBUSY;
+
+		/*
+		 * FIXME: Now, memory hotplug doesn't call shrink_slab() by
+		 * itself. We just check MOVABLE pages.
+		 *
+		 * Pass the intersection of [start_pfn, end_pfn) and the page's
+		 * pageblock to avoid redundant checks.
+		 */
+		check_unmovable_start = max(page_to_pfn(page), start_pfn);
+		check_unmovable_end = min(pageblock_end_pfn(page_to_pfn(page)),
+					  end_pfn);
+
+		unmovable = has_unmovable_pages(check_unmovable_start,
+				check_unmovable_end, mode);
+		if (!unmovable) {
+			if (!pageblock_isolate_and_move_free_pages(zone, page))
+				return -EBUSY;
+			zone->nr_isolate_pageblock++;
+			return 0;
 		}
-		zone->nr_isolate_pageblock++;
-		spin_unlock_irqrestore(&zone->lock, flags);
-		return 0;
 	}
-
-	spin_unlock_irqrestore(&zone->lock, flags);
 	if (mode == PB_ISOLATE_MODE_MEM_OFFLINE) {
 		/*
 		 * printk() with zone->lock held will likely trigger a
Signature · 1 line
2.52.0
#5 lore raw

PATCH RESEND v2 5/8 mm: use zone lock guard in take_page_off_buddy()

In reply to Dmitry Ilvokhin

Use spinlock_irqsave zone lock guard in take_page_off_buddy() to replace the explicit lock/unlock pattern with automatic scope-based cleanup.

This also allows to return directly from the loop, removing the 'ret' variable.

---
 mm/page_alloc.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
mm/page_alloc.c +3 -7
index 0b9a423cbd24..4d074707c850 100644
@@ -7509,11 +7509,9 @@ bool take_page_off_buddy(struct page *page)
 {
 	struct zone *zone = page_zone(page);
 	unsigned long pfn = page_to_pfn(page);
-	unsigned long flags;
 	unsigned int order;
-	bool ret = false;
 
-	spin_lock_irqsave(&zone->lock, flags);
+	guard(spinlock_irqsave)(&zone->lock);
 	for (order = 0; order < NR_PAGE_ORDERS; order++) {
 		struct page *page_head = page - (pfn & ((1 << order) - 1));
 		int page_order = buddy_order(page_head);
@@ -7528,14 +7526,12 @@ bool take_page_off_buddy(struct page *page)
 			break_down_buddy_pages(zone, page_head, page, 0,
 						page_order, migratetype);
 			SetPageHWPoisonTakenOff(page);
-			ret = true;
-			break;
+			return true;
 		}
 		if (page_count(page_head) > 0)
 			break;
 	}
-	spin_unlock_irqrestore(&zone->lock, flags);
-	return ret;
+	return false;
 }
 
 /*
Signature · 1 line
2.52.0
#6 lore raw

PATCH RESEND v2 6/8 mm: use zone lock guard in put_page_back_buddy()

In reply to Dmitry Ilvokhin

Use spinlock_irqsave zone lock guard in put_page_back_buddy() to replace the explicit lock/unlock pattern with automatic scope-based cleanup.

---
 mm/page_alloc.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
mm/page_alloc.c +4 -8
index 4d074707c850..096f5fef0eb5 100644
@@ -7540,23 +7540,19 @@ bool take_page_off_buddy(struct page *page)
 bool put_page_back_buddy(struct page *page)
 {
 	struct zone *zone = page_zone(page);
-	unsigned long flags;
-	bool ret = false;
 
-	spin_lock_irqsave(&zone->lock, flags);
+	guard(spinlock_irqsave)(&zone->lock);
 	if (put_page_testzero(page)) {
 		unsigned long pfn = page_to_pfn(page);
 		int migratetype = get_pfnblock_migratetype(page, pfn);
 
 		ClearPageHWPoisonTakenOff(page);
 		__free_one_page(page, pfn, zone, 0, migratetype, FPI_NONE);
-		if (TestClearPageHWPoison(page)) {
-			ret = true;
-		}
+		if (TestClearPageHWPoison(page))
+			return true;
 	}
-	spin_unlock_irqrestore(&zone->lock, flags);
 
-	return ret;
+	return false;
 }
 #endif
 
Signature · 1 line
2.52.0
#7 lore raw

PATCH RESEND v2 7/8 mm: use zone lock guard in free_pcppages_bulk()

In reply to Dmitry Ilvokhin

Use spinlock_irqsave zone lock guard in free_pcppages_bulk() to replace the explicit lock/unlock pattern with automatic scope-based cleanup.

---
 mm/page_alloc.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
mm/page_alloc.c +1 -4
index 096f5fef0eb5..6a7c548a7406 100644
@@ -1458,7 +1458,6 @@ static void free_pcppages_bulk(struct zone *zone, int count,
 					struct per_cpu_pages *pcp,
 					int pindex)
 {
-	unsigned long flags;
 	unsigned int order;
 	struct page *page;
 
@@ -1471,7 +1470,7 @@ static void free_pcppages_bulk(struct zone *zone, int count,
 	/* Ensure requested pindex is drained first. */
 	pindex = pindex - 1;
 
-	spin_lock_irqsave(&zone->lock, flags);
+	guard(spinlock_irqsave)(&zone->lock);
 
 	while (count > 0) {
 		struct list_head *list;
@@ -1503,8 +1502,6 @@ static void free_pcppages_bulk(struct zone *zone, int count,
 			trace_mm_page_pcpu_drain(page, order, mt);
 		} while (count > 0 && !list_empty(list));
 	}
-
-	spin_unlock_irqrestore(&zone->lock, flags);
 }
 
 /* Split a multi-block free page into its individual pageblocks. */
Signature · 1 line
2.52.0
#8 lore raw

PATCH RESEND v2 8/8 mm: use zone lock guard in __offline_isolated_pages()

In reply to Dmitry Ilvokhin

Use spinlock_irqsave zone lock guard in __offline_isolated_pages() to replace the explicit lock/unlock pattern with automatic scope-based cleanup.

---
 mm/page_alloc.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
mm/page_alloc.c +2 -3
index 6a7c548a7406..bda0282bcb03 100644
@@ -7396,7 +7396,7 @@ void zone_pcp_reset(struct zone *zone)
 unsigned long __offline_isolated_pages(unsigned long start_pfn,
 		unsigned long end_pfn)
 {
-	unsigned long already_offline = 0, flags;
+	unsigned long already_offline = 0;
 	unsigned long pfn = start_pfn;
 	struct page *page;
 	struct zone *zone;
@@ -7404,7 +7404,7 @@ unsigned long __offline_isolated_pages(unsigned long start_pfn,
 
 	offline_mem_sections(pfn, end_pfn);
 	zone = page_zone(pfn_to_page(pfn));
-	spin_lock_irqsave(&zone->lock, flags);
+	guard(spinlock_irqsave)(&zone->lock);
 	while (pfn < end_pfn) {
 		page = pfn_to_page(pfn);
 		/*
@@ -7434,7 +7434,6 @@ unsigned long __offline_isolated_pages(unsigned long start_pfn,
 		del_page_from_free_list(page, zone, order, MIGRATE_ISOLATE);
 		pfn += (1 << order);
 	}
-	spin_unlock_irqrestore(&zone->lock, flags);
 
 	return end_pfn - start_pfn - already_offline;
 }
Signature · 1 line
2.52.0
reviewer Acked-by on the v2 series
#9 lore raw
In reply to Dmitry Ilvokhin
Quoted 49 lines · Dmitry Ilvokhin

Resending v2 to get feedback from folks who work with this code, as Andrew suggested.

This series uses spinlock guard for zone lock across several mm functions to replace explicit lock/unlock patterns with automatic scope-based cleanup.

This simplifies the control flow by removing 'flags' variables, goto labels, and redundant unlock calls.

Patches are ordered by decreasing value. The first six patches simplify the control flow by removing gotos, multiple unlock paths, or 'ret' variables. The last two are simpler lock/unlock pair conversions that only remove 'flags' and can be dropped if considered unnecessary churn.

Based on mm-stable.

v1 -> v2:

- Andrew Morton raised concerns about binary size increase in v1. Peter Zijlstra has since fixed the underlying issue in the guards infrastructure in tip [1]. Note: the fix is not yet in mm-stable, so it needs to be applied first to reproduce these results. With that fix, bloat-o-meter on x86 defconfig shows a net decrease of 49 bytes (-0.12%) for page_alloc.o.
- Rebased on mm-stable, since the patch this series depended on was dropped from mm-new.
- Converted guard(zone_lock_irqsave)(zone) to guard(spinlock_irqsave)(&zone->lock).
- Dropped redundant braces in unreserve_highatomic_pageblock() (Steven Rostedt)

v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/

[1]: https://lore.kernel.org/all/[email protected]/

Dmitry Ilvokhin (8):
mm: use zone lock guard in reserve_highatomic_pageblock()
mm: use zone lock guard in unset_migratetype_isolate()
mm: use zone lock guard in unreserve_highatomic_pageblock()
mm: use zone lock guard in set_migratetype_isolate()
mm: use zone lock guard in take_page_off_buddy()
mm: use zone lock guard in put_page_back_buddy()
mm: use zone lock guard in free_pcppages_bulk()
mm: use zone lock guard in __offline_isolated_pages()

Quoted 3 lines
 mm/page_alloc.c     | 53 ++++++++++++-----------------------
 mm/page_isolation.c | 67 +++++++++++++++++++--------------------------
 2 files changed, 45 insertions(+), 75 deletions(-)

I like the resulting code. For the whole series.

Signature · 2 lines
Michal Hocko
SUSE Labs
maintainer
#10 lore raw
Quoted 10 lines · On Wed, 8 Apr 2026 18:42:16 +0200 Michal Hocko <[email protected]> wrote:
Quoted 10 lines

Dmitry Ilvokhin (8):
mm: use zone lock guard in reserve_highatomic_pageblock()
mm: use zone lock guard in unset_migratetype_isolate()
mm: use zone lock guard in unreserve_highatomic_pageblock()
mm: use zone lock guard in set_migratetype_isolate()
mm: use zone lock guard in take_page_off_buddy()
mm: use zone lock guard in put_page_back_buddy()
mm: use zone lock guard in free_pcppages_bulk()
mm: use zone lock guard in __offline_isolated_pages()

Quoted 6 lines
Quoted 3 lines
 mm/page_alloc.c     | 53 ++++++++++++-----------------------
 mm/page_isolation.c | 67 +++++++++++++++++++--------------------------
 2 files changed, 45 insertions(+), 75 deletions(-)

I like the resulting code. For the whole series.

Thanks.

As I mentioned previously, the outcome here hinges on how the developers who work on this code feel about using guard(). Most of them are in hiding at present but that's OK - it's post -rc1 fun.

#11 lore raw
Quoted 16 lines · Andrew Morton
Quoted 14 lines · On Wed, 8 Apr 2026 18:42:16 +0200 Michal Hocko <[email protected]> wrote:
Quoted 13 lines

Dmitry Ilvokhin (8):
mm: use zone lock guard in reserve_highatomic_pageblock()
mm: use zone lock guard in unset_migratetype_isolate()
mm: use zone lock guard in unreserve_highatomic_pageblock()
mm: use zone lock guard in set_migratetype_isolate()
mm: use zone lock guard in take_page_off_buddy()
mm: use zone lock guard in put_page_back_buddy()
mm: use zone lock guard in free_pcppages_bulk()
mm: use zone lock guard in __offline_isolated_pages()

 mm/page_alloc.c     | 53 ++++++++++++-----------------------
 mm/page_isolation.c | 67 +++++++++++++++++++--------------------------
 2 files changed, 45 insertions(+), 75 deletions(-)
Quoted 9 lines
Quoted 2 lines

I like the resulting code. For the whole series.

Thanks.

As I mentioned previously, the outcome here hinges on how the developers who work on this code feel about using guard(). Most of them are in hiding at present but that's OK - it's post -rc1 fun.

Thanks, Andrew.

Understood. I'll wait for feedback from more folks. Hopefully they reappear soon after the -rc1 dust settles.