[RFC PATCH 1/6] PM: hibernate: factor out zone_usable_free_pages()

From: Nhat-Trieu Huynh-Pham

Date: Sat Nov 28 2026 - 14:31:54 EST


hibernate_preallocate_memory() computes the order-0 non-movable usable
free pages in a zone by excluding MIGRATE_HIGHATOMIC and free CMA
pageblocks (with vmstat underflow clamping). Extract this calculation
into zone_usable_free_pages() so the upcoming compress-on-copy
preallocation fallback can share the same accounting without duplicating
it.

No functional change.

Signed-off-by: Nhat-Trieu Huynh-Pham <trieu2.huynh@xxxxxxx>
---
kernel/power/snapshot.c | 53 +++++++++++++++++++++++------------------
1 file changed, 30 insertions(+), 23 deletions(-)

diff --git a/kernel/power/snapshot.c b/kernel/power/snapshot.c
index 867fcaf8d032..d06a52a7566b 100644
--- a/kernel/power/snapshot.c
+++ b/kernel/power/snapshot.c
@@ -1799,6 +1799,32 @@ static unsigned long minimum_image_size(unsigned long saveable)
return saveable <= size ? 0 : saveable - size;
}

+static unsigned long zone_usable_free_pages(struct zone *zone)
+{
+ unsigned long free, unusable;
+
+ /*
+ * Pages reserved for high-order atomic allocations
+ * (MIGRATE_HIGHATOMIC) and free CMA pages cannot be used by
+ * the order-0 non-movable GFP_KERNEL allocations that
+ * preallocate the image, so do not count them as available.
+ * This mirrors __zone_watermark_unusable_free().
+ */
+ free = zone_page_state(zone, NR_FREE_PAGES);
+ unusable = READ_ONCE(zone->nr_free_highatomic);
+#ifdef CONFIG_CMA
+ unusable += zone_page_state(zone, NR_FREE_CMA_PAGES);
+#endif
+ /*
+ * free is a per-CPU vmstat approximation and is not
+ * sampled atomically with unusable, so free - unusable
+ * can transiently underflow. Clamp unusable to free.
+ */
+ unusable = min(unusable, free);
+
+ return free - unusable;
+}
+
/**
* hibernate_preallocate_memory - Preallocate memory for hibernation image.
*
@@ -1867,33 +1893,14 @@ int hibernate_preallocate_memory(void)
highmem = save_highmem;
size = 0;
for_each_populated_zone(zone) {
- unsigned long free, unusable;
+ unsigned long free;

size += snapshot_additional_pages(zone);
-
- /*
- * Pages reserved for high-order atomic allocations
- * (MIGRATE_HIGHATOMIC) and free CMA pages cannot be used by
- * the order-0 non-movable GFP_KERNEL allocations that
- * preallocate the image, so do not count them as available.
- * This mirrors __zone_watermark_unusable_free().
- */
- free = zone_page_state(zone, NR_FREE_PAGES);
- unusable = READ_ONCE(zone->nr_free_highatomic);
-#ifdef CONFIG_CMA
- unusable += zone_page_state(zone, NR_FREE_CMA_PAGES);
-#endif
- /*
- * free is a per-CPU vmstat approximation and is not
- * sampled atomically with unusable, so free - unusable
- * can transiently underflow. Clamp unusable to free.
- */
- unusable = min(unusable, free);
-
+ free = zone_usable_free_pages(zone);
if (is_highmem(zone))
- highmem += free - unusable;
+ highmem += free;
else
- count += free - unusable;
+ count += free;
}
avail_normal = count;
count += highmem;
--
2.43.0