Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Youngjun Park <youngjun.park@lge.com>
To: Andrew Morton <akpm@linux-foundation.org>,
	"Rafael J. Wysocki" <rafael@kernel.org>,
	Kairui Song <kasong@tencent.com>, Chris Li <chrisl@kernel.org>
Cc: Kemeng Shi <shikemeng@huaweicloud.com>,
	Nhat Pham <nphamcs@gmail.com>, Baoquan He <baoquan.he@linux.dev>,
	Barry Song <baohua@kernel.org>, Pavel Machek <pavel@kernel.org>,
	Len Brown <lenb@kernel.org>,
	linux-mm@kvack.org, linux-pm@vger.kernel.org,
	her0gyugyu@gmail.com, youngjun.park@lge.com,
	taejoon.song@lge.com
Subject: [RFC PATCH 10/10] mm, swap: hold swap space back for a hibernation image at swapon
Date: Tue, 15 Sep 2026 12:16:58 +0900	[thread overview]
Message-ID: <20260915031658.1505680-11-youngjun.park@lge.com> (raw)
In-Reply-To: <20260915031658.1505680-1-youngjun.park@lge.com>

Hibernation takes whatever clusters are free when it runs, and an aged
device may have no long run of them.

At swapon, ask hibernation_reserve_pages() how much to hold back and set
aside that many empty clusters in a row.  Nothing uses the device yet,
so this needs no locking.  The run leaves si->pages and goes on no list,
so the allocator never sees it.  Hibernation hands it out whole, before
any other slot.  More than half of a device is refused.

Assisted-by: Claude:claude-opus-5
Signed-off-by: Youngjun Park <youngjun.park@lge.com>
---
 include/linux/swap.h |  2 ++
 mm/swap_hibernate.c  | 78 ++++++++++++++++++++++++++++++++++++++++----
 mm/swapfile.c        | 23 ++++++++++++-
 3 files changed, 95 insertions(+), 8 deletions(-)

diff --git a/include/linux/swap.h b/include/linux/swap.h
index 94c894c7ad9d..66d59828b327 100644
--- a/include/linux/swap.h
+++ b/include/linux/swap.h
@@ -249,6 +249,8 @@ struct swap_info_struct {
 					/* list of cluster that are fragmented or contented */
 	unsigned int pages;		/* total of usable pages of swap */
 	atomic_long_t inuse_pages;	/* number of those currently in use */
+	unsigned int hib_reserve_start;	/* clusters held back for hibernation */
+	unsigned int hib_reserve_nr;	/* how many, 0 for none */
 	struct swap_sequential_cluster *global_cluster; /* Use one global cluster for rotating device */
 	spinlock_t global_cluster_lock;	/* Serialize usage of global cluster */
 	struct rb_root swap_extent_root;/* root of the swap extent rbtree */
diff --git a/mm/swap_hibernate.c b/mm/swap_hibernate.c
index 23dc766f04cd..7475ae0966f3 100644
--- a/mm/swap_hibernate.c
+++ b/mm/swap_hibernate.c
@@ -5,10 +5,12 @@
  */
 
 /*
- * The image gets whole free clusters first, then single slots from the
- * allocator.  Clusters it does not use go back when the session ends.
+ * The image gets the run held at swapon first, then whole free clusters,
+ * then single slots from the allocator.  Free clusters it does not use go
+ * back when the session ends.
  */
 static struct swap_info_struct *hib_si;
+static unsigned int hib_reserve;	/* clusters held back, still to hand out */
 
 /* Free clusters set aside for the image, out of the allocator's reach. */
 static LIST_HEAD(hib_free_clusters);
@@ -38,6 +40,56 @@ static void hib_return_free_clusters(struct swap_info_struct *si)
 	spin_unlock(&si->lock);
 }
 
+static bool hib_cluster_reserved(struct swap_info_struct *si, unsigned int idx)
+{
+	return idx >= si->hib_reserve_start &&
+	       idx < si->hib_reserve_start + si->hib_reserve_nr;
+}
+
+/*
+ * Hold a run of empty clusters back for the image.  This runs at swapon
+ * before the device is in use, so it needs no lock.
+ *
+ * Return: the number of pages held, to be taken out of si->pages.
+ */
+static unsigned long hib_reserve_at_swapon(struct swap_info_struct *si,
+					   struct swap_cluster_info *cluster_info,
+					   unsigned long nr_clusters)
+{
+	unsigned long want, run = 0, i;
+
+	si->hib_reserve_start = 0;
+	si->hib_reserve_nr = 0;
+
+	if (!si->bdev)
+		return 0;
+	/* Swap files sharing a block device differ by their first block. */
+	want = DIV_ROUND_UP(hibernation_reserve_pages(si->bdev->bd_dev,
+						      first_se(si)->start_block),
+			    SWAPFILE_CLUSTER);
+	if (!want)
+		return 0;
+	if (want > nr_clusters / 2) {
+		pr_warn("swapon: %lu clusters for the hibernation image is more than half of the device\n",
+			want);
+		return 0;
+	}
+
+	for (i = 0; i < nr_clusters && run < want; i++)
+		run = cluster_info[i].count ? 0 : run + 1;
+	if (run < want) {
+		pr_warn("swapon: no run of %lu clusters for the hibernation image\n",
+			want);
+		return 0;
+	}
+
+	si->hib_reserve_start = i - want;
+	si->hib_reserve_nr = want;
+	pr_info("swapon: holding %lu clusters at %u back for the hibernation image\n",
+		want, si->hib_reserve_start);
+	return want * SWAPFILE_CLUSTER;
+}
+
 /*
  * Take a whole free cluster.  It gets no swap table, so cluster_is_usable()
  * keeps the allocator away from it.
@@ -130,6 +182,7 @@ static void hib_session_begin(struct swap_info_struct *si)
 	if (hib_si)
 		hib_return_free_clusters(hib_si);
 	hib_si = si;
+	hib_reserve = si->hib_reserve_nr;
 }
 
 static void hib_session_end(void)
@@ -189,6 +242,13 @@ swp_entry_t swap_alloc_hibernation_slot(unsigned int *nr)
 	if (WARN_ON_ONCE(!si))
 		return entry;
 
+	if (hib_reserve) {
+		*nr = hib_reserve * SWAPFILE_CLUSTER;
+		hib_reserve = 0;
+		return swp_entry(si->type,
+				 si->hib_reserve_start * SWAPFILE_CLUSTER);
+	}
+
 	len = hib_take_free(si, &start);
 	if (!len) {
 		start = hib_alloc_slot(si);
@@ -218,10 +278,13 @@ void swap_free_hibernation_slot(swp_entry_t entry, unsigned int nr)
 		ci_off = offset % SWAPFILE_CLUSTER;
 		count = min(nr, SWAPFILE_CLUSTER - ci_off);
 		ci = swap_cluster_lock(si, offset);
-		if (cluster_table_is_alloced(ci))
-			__swap_cluster_free_entries(si, ci, ci_off, count);
-		else
-			hib_put_cluster_slots(si, ci, ci_off, count);
+		/* A cluster held for the image is left as it is. */
+		if (!hib_cluster_reserved(si, cluster_index(si, ci))) {
+			if (cluster_table_is_alloced(ci))
+				__swap_cluster_free_entries(si, ci, ci_off, count);
+			else
+				hib_put_cluster_slots(si, ci, ci_off, count);
+		}
 		swap_cluster_unlock(ci);
 		offset += count;
 		nr -= count;
@@ -472,7 +535,8 @@ unsigned int count_swap_pages(int type, int free)
 
 		spin_lock(&sis->lock);
 		if (sis->flags & SWP_WRITEOK) {
-			n = sis->pages;
+			/* the run held back is there for the image */
+			n = sis->pages + sis->hib_reserve_nr * SWAPFILE_CLUSTER;
 			if (free)
 				n -= swap_usage_in_pages(sis);
 		}
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 98ed75708661..3b79cec6fd27 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -2182,6 +2182,18 @@ void swap_put_entries_direct(swp_entry_t entry, int nr)
 
 #ifdef CONFIG_HIBERNATION
 #include "swap_hibernate.c"
+#else
+static unsigned long hib_reserve_at_swapon(struct swap_info_struct *si,
+					   struct swap_cluster_info *cluster_info,
+					   unsigned long nr_clusters)
+{
+	return 0;
+}
+
+static bool hib_cluster_reserved(struct swap_info_struct *si, unsigned int idx)
+{
+	return false;
+}
 #endif /* CONFIG_HIBERNATION */
 
 static inline int pte_same_as_swp(pte_t pte, pte_t swp_pte)
@@ -3379,6 +3391,13 @@ static int setup_swap_clusters_info(struct swap_info_struct *si,
 			goto err;
 	}
 
+	/*
+	 * Whatever is held back for a hibernation image leaves si->pages, so
+	 * that it is never advertised as swap, and it goes on no list below,
+	 * so that the allocator cannot find it.
+	 */
+	si->pages -= hib_reserve_at_swapon(si, cluster_info, nr_clusters);
+
 	INIT_LIST_HEAD(&si->free_clusters);
 	INIT_LIST_HEAD(&si->full_clusters);
 	INIT_LIST_HEAD(&si->discard_clusters);
@@ -3391,7 +3410,9 @@ static int setup_swap_clusters_info(struct swap_info_struct *si,
 	for (i = 0; i < nr_clusters; i++) {
 		struct swap_cluster_info *ci = &cluster_info[i];
 
-		if (ci->count) {
+		if (hib_cluster_reserved(si, i)) {
+			ci->flags = CLUSTER_FLAG_NONE;
+		} else if (ci->count) {
 			ci->flags = CLUSTER_FLAG_NONFULL;
 			list_add_tail(&ci->list, &si->nonfull_clusters[0]);
 		} else {
-- 
2.48.1



  parent reply	other threads:[~2026-09-15  3:17 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-15  3:16 [RFC PATCH 00/10] mm/swap, PM: hibernate: improve image slot allocation and I/O Youngjun Park
2026-09-15  3:16 ` [RFC PATCH 01/10] PM: hibernate: give the image's swap slots back when test_resume fails Youngjun Park
2026-09-15  3:16 ` [RFC PATCH 02/10] mm, swap: skip swap devices without a block device in hibernation lookups Youngjun Park
2026-09-15  3:16 ` [RFC PATCH 03/10] mm, swap: move hibernation swap code to mm/swap_hibernate.c Youngjun Park
2026-09-15  3:16 ` [RFC PATCH 04/10] mm, swap: skip swap cache reclaim while storage is suspended Youngjun Park
2026-09-15  3:16 ` [RFC PATCH 05/10] mm, swap: hand the hibernation image whole free clusters Youngjun Park
2026-09-15  3:16 ` [RFC PATCH 06/10] mm, swap: hand the image's free clusters out in disk order Youngjun Park
2026-09-15  3:16 ` [RFC PATCH 07/10] PM: hibernate: build one bio per contiguous run of the image Youngjun Park
2026-09-15  3:16 ` [RFC PATCH 08/10] PM: hibernate: read the image back a run at a time Youngjun Park
2026-09-15  3:16 ` [RFC PATCH 09/10] PM: hibernate: tell swap how much space an image needs Youngjun Park
2026-09-15  3:16 ` Youngjun Park [this message]
2026-09-29 17:25 ` [RFC PATCH 00/10] mm/swap, PM: hibernate: improve image slot allocation and I/O Kairui Song
2026-10-04 17:39   ` Youngjun Park

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260915031658.1505680-11-youngjun.park@lge.com \
    --to=youngjun.park@lge.com \
    --cc=akpm@linux-foundation.org \
    --cc=baohua@kernel.org \
    --cc=baoquan.he@linux.dev \
    --cc=chrisl@kernel.org \
    --cc=her0gyugyu@gmail.com \
    --cc=kasong@tencent.com \
    --cc=lenb@kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=nphamcs@gmail.com \
    --cc=pavel@kernel.org \
    --cc=rafael@kernel.org \
    --cc=shikemeng@huaweicloud.com \
    --cc=taejoon.song@lge.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox