Skip to contentSkip to conversation

mm/swap, PM: hibernate: improve image slot allocation and I/O

RFC PATCH In review Youngjun Park 3 participants 13 messages Sep 15 – Oct 4

Subsystems (5)
lore
Share & tools
lore

Youngjun Park

author reviewer
#0 lore raw

cover mm/swap, PM: hibernate: improve image slot allocation and I/O

This series improves how hibernation allocates swap slots for its image. It starts from a few observations about what happens while the image is written. With them the allocator can hand the image contiguous runs, the image I/O can be batched per run, and hibernation gets faster. Contiguous I/O pattern is friendly to flash device also.

Slot allocation matters for hibernation speed. After commit 0ff67f990bd4 ("mm, swap: remove swap slot cache") in v6.15, writing the image was about ten times slower on some SSDs [1], until commit 396f57b57200 ("mm, swap: speed up hibernation allocation and writeout") fixed it in v7.1.

Note
- I was responsible for the observation and design,
while receiving substantial support from LLMs throughout the series.
- Submitting this patch to confirm whether this work is progressing or not.

Observations
============

1. While the image is written, swap can only hand out slots that are already free. Swap cache cannot be reclaimed to make more.

folio_swapcache_freeable() refuses every folio while storage is suspended. The image may already hold a folio as clean swap cache. If its slot were freed and reused for the image, the resumed kernel could later drop that folio and read it back from the slot, now with the wrong data.

Storage is suspended before the image is written, so every allocation for the image falls in that window.

hibernate()
freeze_processes() user space is frozen
hibernation_snapshot()
freeze_kernel_threads() kswapd is frozen
hibernate_preallocate_memory() may swap out to shrink memory
pm_restrict_gfp_mask() storage is suspended from here
create_image() the snapshot is taken
swsusp_write() image slots are allocated here
power_down()

uswsusp can send SNAPSHOT_ALLOC_SWAP_PAGE before storage is suspended, but s2disk sends it only after SNAPSHOT_CREATE_IMAGE [2]. An early request is still served by the normal allocator.

So the image does not need to walk the nonfull and frag clusters. It can take the free clusters first and use the others only when those run out.

2. Image slots are not freed and reused while the image is written. And whatever the allocator changes during the write is gone at resume, because the resumed kernel is the snapshot.

With observation 1, once the image owns a free cluster it can use all of it. Nothing has to be recorded per slot, neither in the swap table nor as a memcg id.

3. User space and kswapd do not swap out while the image is written, and allocations from the page allocator cannot start swap I/O. What is left is rare. DAMON pageout and the memcg high work can still reach swap(This is all I found. anything else?), and both were seen running in that window.

So the allocator can favor the image then. Other users take slots from the nonfull and frag clusters and leave the free clusters to the image.

With these the allocator gets simpler and faster, and batching the image read and write becomes easy.

What the series does
====================

1-2 fixes, a slot leak after a failed test_resume and a NULL
dereference for a swapfile with no block device (some bug fix)

  3      move the hibernation code to mm/swap_hibernate.c (refactor)
  4      skip swap cache reclaim while storage is suspended (optimization)
  5-6    hand the image whole free clusters, in disk order (exploit contiguous space)
  7-8    write and read the image one bio per run (batch I/O)
  9-10   optional reservation at swapon, hibernate=reserve (assure contiguous space)

Based on mm-new (383fc05d4650) with patches 2 to 4 of [3] under it. Patch 1 of [3] is in mm-new as 10d9012e83ef.

Note. [3] gives hibernation slots their own swap table entry, keeps readahead off them, and frees them by offset alone. The single slot path of patch 5 builds on that.

Results
=======

Setup
- qemu, 12G RAM, 4 CPUs, no KVM. Times only compare against each other.
- swap on virtio-blk as a non-rotational device
- image 5.0G, written with hibernate=nocompress
- 3 reps of two hibernations each. Times are medians of the 4 to 6 samples per cell that no host load hit.
- base is patch 3 and allocates as mm-new does, allocator is patch 6, allocator + bio is patch 8

Rows. runs is how many contiguous stretches of the device the image ends up in. bios is how many bios the kernel allocates and submits to write it. In both cases the image fits in free clusters, so the fallback to nonfull and frag clusters is not measured. Percentages are against base.

Shuffled free list. A device that has been in use, emptied.
- 6G swap, 5.4G of 2M tmpfs files swapped out, then all removed in random order
- every cluster is free, the free list is in free order, not in disk order

base allocator allocator + bio

  write, s             10.60     9.32 (-12%)       7.94 (-25%)
  read, s               9.15     8.92  (-3%)       8.10 (-11%)

runs 3560 1 1
bios 1.30M 1.30M 12.7K

Holes in nonfull clusters. What taking free clusters first buys.
- 12G swap, one 4G file swapped out, every other 64K of it freed
- 2G of 64K holes in 2048 clusters, 8G of free clusters
- mm-new fills the holes first, the series takes the free clusters

base allocator allocator + bio

  write, s             10.69    10.07  (-6%)       8.81 (-18%)
  read, s              12.21    10.89 (-11%)       9.78 (-20%)
  runs                 31899               1                 1
  bios                 1.30M           1.30M             12.8K

Summary against base
- the allocator cuts write time by 6 to 12%
- allocator + bio cuts write time by 18 to 25% and read time by

    11 to 20%

These are VM numbers without compression. Compression, the default, and real hardware are still to be checked.

Next steps
==========

Things to keep working on after this RFC. Comments are welcome.

1. Dropping swap cache before hibernation starts, so more slots are free. This series does not do that.

2. Whether the reservation in patches 9 and 10 is worth keeping. It makes sure the image gets contiguous slots when swap has room to spare.

3. A block device of its own for hibernation instead of swap. Not taken for now. Sharing one device keeps the spare space useful, the existing infrastructure stays, and the ideas above give much the same effect.

4. Whether the extent tree can go. A normal hibernation never walks it, the swap state comes back as it was at the snapshot. It is only walked to free the slots after an error or a wake from hybrid sleep. With the slots marked in the swap table [3] and taken as whole clusters, a free could find them without it.

5. Whether SNAPSHOT_ALLOC_SWAP_PAGE should refuse a request made before storage is suspended. Such a request gets single slots from the normal allocator today, and s2disk only asks after SNAPSHOT_CREATE_IMAGE anyway.

6. Two cases are not measured yet. A device with both a shuffled free list and partly used clusters. An image bigger than the free clusters, so part of it comes from nonfull and frag clusters.

[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://git.kernel.org/pub/scm/linux/kernel/git/rafael/suspend-utils.git
[3] https://lore.kernel.org/linux-mm/[email protected]/

Youngjun Park (10):
PM: hibernate: give the image's swap slots back when test_resume fails mm, swap: skip swap devices without a block device in hibernation
lookups
mm, swap: move hibernation swap code to mm/swap_hibernate.c mm, swap: skip swap cache reclaim while storage is suspended mm, swap: hand the hibernation image whole free clusters mm, swap: hand the image's free clusters out in disk order
PM: hibernate: build one bio per contiguous run of the image
PM: hibernate: read the image back a run at a time
PM: hibernate: tell swap how much space an image needs
mm, swap: hold swap space back for a hibernation image at swapon

 .../admin-guide/kernel-parameters.txt         |   8 +
 MAINTAINERS                                   |   1 +
 include/linux/suspend.h                       |   3 +
 include/linux/swap.h                          |  11 +-
 kernel/power/hibernate.c                      |  32 +-
 kernel/power/power.h                          |   1 +
 kernel/power/swap.c                           | 172 +++++-
 mm/swap_hibernate.c                           | 547 ++++++++++++++++++
 mm/swapfile.c                                 | 344 ++---------
 9 files changed, 783 insertions(+), 336 deletions(-)
 create mode 100644 mm/swap_hibernate.c

base-commit: 383fc05d4650b021f3c17e36a145106dcc61a294

Signature · 1 line
2.48.1
author reviewer
#1 lore raw

RFC PATCH 1/10 PM: hibernate: give the image's swap slots back when test_resume fails

test_resume writes an image and loads it straight back without powering off. When the load fails nothing gives the image's swap slots back. They stay allocated for as long as the system runs, and swapoff on that device cannot finish, since the slots belong to no process.

Tested by forcing the restore to fail in test_resume mode.

Fixes fe12c00d21bb ("PM / hibernate: Introduce test_resume mode for hibernation")
Assisted-by Claude:claude-fable-5
---
 kernel/power/hibernate.c |  6 +++++-
 kernel/power/power.h     |  1 +
 kernel/power/swap.c      | 12 ++++++++++++
 3 files changed, 18 insertions(+), 1 deletion(-)
kernel/power/hibernate.c +5 -1
index d2479c69d71a..907d791b85ad 100644
@@ -857,8 +857,12 @@ int hibernate(void)
 	if (snapshot_test) {
 		pm_pr_dbg("Checking hibernation image\n");
 		error = swsusp_check(false);
-		if (!error)
+		if (!error) {
 			error = load_image_and_restore();
+			/* The image was not used, so free its slots. */
+			if (error)
+				swsusp_free_image_slots();
+		}
 	}
 	thaw_processes();
 
kernel/power/power.h +1 -0
index 75b63843886e..9c398cddd0c3 100644
@@ -171,6 +171,7 @@ extern void hibernate_release(void);
 extern sector_t alloc_swapdev_block(int swap);
 extern void free_all_swap_pages(int swap);
 extern int swsusp_swap_in_use(void);
+void swsusp_free_image_slots(void);
 
 /*
  * Flags that can be passed from the hibernatig hernel to the "boot" kernel in
kernel/power/swap.c +12 -0
index c78f1593600b..dc65ec409207 100644
@@ -1612,6 +1612,18 @@ int swsusp_check(bool exclusive)
 	return error;
 }
 
+/**
+ * swsusp_free_image_slots - Give back the swap slots the image was written to.
+ *
+ * For a path that writes an image and then does not resume from it.  Until
+ * the slots are given back they stay allocated, and swapoff on that device
+ * cannot finish either.
+ */
+void swsusp_free_image_slots(void)
+{
+	free_all_swap_pages(root_swap);
+}
+
 /**
  * swsusp_close - close resume device.
  */
Signature · 1 line
2.48.1
author reviewer
#2 lore raw

RFC PATCH 2/10 mm, swap: skip swap devices without a block device in hibernation lookups

In reply to Youngjun Park

A swapfile on NFS or SMB3 has no block device, so claim_swapfile() leaves si->bdev NULL. __find_hibernation_swap_type() and find_first_swap() dereference it without checking.

Hibernation cannot use such a device anyway. Skip it.

Fixes 21bd900572f3 ("mm: split swap_type_of")
Assisted-by Claude:claude-fable-5
---
 mm/swapfile.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/swapfile.c +2 -2
index 592ca26a5c0b..655f2647472c 100644
@@ -2238,7 +2238,7 @@ static int __find_hibernation_swap_type(dev_t device, sector_t offset)
 	for (type = 0; type < nr_swapfiles; type++) {
 		struct swap_info_struct *sis = swap_info[type];
 
-		if (!(sis->flags & SWP_WRITEOK))
+		if (!(sis->flags & SWP_WRITEOK) || !sis->bdev)
 			continue;
 
 		if (device == sis->bdev->bd_dev) {
@@ -2425,7 +2425,7 @@ int find_first_swap(dev_t *device)
 	for (type = 0; type < nr_swapfiles; type++) {
 		struct swap_info_struct *sis = swap_info[type];
 
-		if (!(sis->flags & SWP_WRITEOK))
+		if (!(sis->flags & SWP_WRITEOK) || !sis->bdev)
 			continue;
 		*device = sis->bdev->bd_dev;
 		spin_unlock(&swap_lock);
Signature · 1 line
2.48.1
author reviewer
#3 lore raw

RFC PATCH 3/10 mm, swap: move hibernation swap code to mm/swap_hibernate.c

In reply to Youngjun Park

The hibernation parts of mm/swapfile.c are mixed in with the allocator. Move them to their own file so they are easier to read and maintain.

The file is #included from swapfile.c, so the allocator's data structures stay private.

No functional change.

Assisted-by Claude:claude-opus-5
---
 MAINTAINERS         |   1 +
 mm/swap_hibernate.c | 316 ++++++++++++++++++++++++++++++++++++++++++++
 mm/swapfile.c       | 311 +------------------------------------------
 3 files changed, 318 insertions(+), 310 deletions(-)
 create mode 100644 mm/swap_hibernate.c
MAINTAINERS +1 -0
index e4412c3d8d45..8273f220ae90 100644
@@ -17408,6 +17408,7 @@ F:	include/linux/swapfile.h
 F:	include/linux/swapops.h
 F:	mm/page_io.c
 F:	mm/swap.h
+F:	mm/swap_hibernate.c
 F:	mm/swap_table.h
 F:	mm/swap_state.c
 F:	mm/swapfile.c
mm/swap_hibernate.c new file +316 -0

316 lines not on this page · raw message

mm/swapfile.c +1 -310

317 lines not on this page · raw message

Signature · 1 line
2.48.1
author reviewer
#4 lore raw

RFC PATCH 4/10 mm, swap: skip swap cache reclaim while storage is suspended

In reply to Youngjun Park

folio_swapcache_freeable() refuses every folio for as long as pm_suspended_storage() holds, and that covers the whole window in which the hibernation image is written. Every reclaim attempt the allocator makes there drops the cluster lock, looks the folio up, trylocks it and gives up. swap_reclaim_full_clusters() walks a whole cluster to do it.

Stop trying. The allocator is left with the slots that are already free, which is all reclaim could have given it anyway.

Assisted-by Claude:claude-opus-5
---
 mm/swapfile.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
mm/swapfile.c +6 -2
index 07aefb782fbe..cf519c5569c4 100644
@@ -903,7 +903,7 @@ static bool cluster_scan_range(struct swap_info_struct *si,
 		if (swp_tb_is_null(swp_tb))
 			continue;
 		if (swp_tb_is_folio(swp_tb) && !__swp_tb_get_count(swp_tb)) {
-			if (!vm_swap_full())
+			if (!vm_swap_full() || pm_suspended_storage())
 				return false;
 			*need_reclaim = true;
 			continue;
@@ -1050,6 +1050,10 @@ static void swap_reclaim_full_clusters(struct swap_info_struct *si, bool force)
 	unsigned long swp_tb;
 	int nr_reclaim;
 
+	/* Swap cache cannot be freed while storage is suspended. */
+	if (pm_suspended_storage())
+		return;
+
 	if (force)
 		to_scan = swap_usage_in_pages(si) / SWAPFILE_CLUSTER;
 
@@ -1315,7 +1319,7 @@ static void swap_range_alloc(struct swap_info_struct *si,
 			     unsigned int nr_entries)
 {
 	if (swap_usage_add(si, nr_entries)) {
-		if (vm_swap_full())
+		if (vm_swap_full() && !pm_suspended_storage())
 			schedule_work(&si->reclaim_work);
 	}
 	atomic_long_sub(nr_entries, &nr_swap_pages);
Signature · 1 line
2.48.1
author reviewer
#5 lore raw

RFC PATCH 5/10 mm, swap: hand the hibernation image whole free clusters

In reply to Youngjun Park

Hibernation takes its slots from the allocator one at a time, so the image lands wherever swap has holes and is written in many pieces.

When the image asks for slots, move the free clusters to a private list and give each one out whole, as a run of contiguous slots. What the image does not use goes back to the free list when the writer is done, whether the write worked or not.

These clusters get no swap table and no memcg table, since the image does not need them. After a successful write the machine powers off and the system resumes from a snapshot taken before the slots were handed out, so they never have to be given back. After an error or a hybrid sleep they do come back, and the cluster count is enough for that. Taking a cluster is then cheap, with nothing to allocate.

Taking all of them is fine because this only happens once storage is suspended. By then user space and kswapd are frozen, so swap allocation is rare, and writing the image fast is what matters. Any allocation that does happen uses the nonfull and frag lists. A uswsusp tool can call SNAPSHOT_ALLOC_SWAP_PAGE before that, and those slots come from the allocator as before.

When the free clusters run out, the image shares the allocator with everyone else and takes a slot at a time from the nonfull and frag lists, as before. Those clusters could be set aside too, but other users still free slots in them, and a free can move a cluster to another list. The image would then have to stay in sync with those moves, which the allocator already does.

Assisted-by Claude:claude-opus-5
---
 include/linux/swap.h |   9 +-
 kernel/power/swap.c  |  63 +++++++++---
 mm/swap_hibernate.c  | 222 ++++++++++++++++++++++++++++++++++++-------
 mm/swapfile.c        |  11 +++
 4 files changed, 256 insertions(+), 49 deletions(-)
include/linux/swap.h +6 -3
index 78974da6810e..94c894c7ad9d 100644
@@ -423,9 +423,12 @@ void swap_put_entries_direct(swp_entry_t entry, int nr);
  */
 bool folio_free_swap(struct folio *folio);
 
-/* Allocate / free (hibernation) exclusive entries */
-swp_entry_t swap_alloc_hibernation_slot(int type);
-void swap_free_hibernation_slot(swp_entry_t entry);
+/* Hibernation takes runs of slots off the device it is bound to */
+int swap_hibernation_bind(int type);
+void swap_hibernation_unbind(void);
+void swap_hibernation_reset(void);
+swp_entry_t swap_alloc_hibernation_slot(unsigned int *nr);
+void swap_free_hibernation_slot(swp_entry_t entry, unsigned int nr);
 
 static inline void put_swap_device(struct swap_info_struct *si)
 {
kernel/power/swap.c +49 -14

111 lines not on this page · raw message

mm/swap_hibernate.c +190 -32

272 lines not on this page · raw message

mm/swapfile.c +11 -0
index cf519c5569c4..86ed8c6adae7 100644
@@ -514,6 +514,10 @@ static void swap_cluster_assert_empty(struct swap_cluster_info *ci,
 	if (!IS_ENABLED(CONFIG_DEBUG_VM) && !swapoff)
 		return;
 
+	/* A cluster that has no table has no entries to look at. */
+	if (!cluster_table_is_alloced(ci))
+		return;
+
 	do {
 		swp_tb = __swap_table_get(ci, ci_off);
 		if (swp_tb_is_bad(swp_tb))
@@ -777,6 +781,13 @@ static void relocate_cluster(struct swap_info_struct *si,
 		if (ci->flags != CLUSTER_FLAG_FREE)
 			free_cluster(si, ci);
 	} else if (ci->count != SWAPFILE_CLUSTER) {
+		/*
+		 * A cluster taken whole without a table has nothing to
+		 * allocate from, so it waits off the lists until all of it
+		 * has been given back.
+		 */
+		if (!cluster_table_is_alloced(ci))
+			return;
 		if (ci->flags != CLUSTER_FLAG_FRAG)
 			move_cluster(si, ci, &si->frag_clusters[ci->order],
 				     CLUSTER_FLAG_FRAG);
Signature · 1 line
2.48.1
author reviewer
#6 lore raw

RFC PATCH 6/10 mm, swap: hand the image's free clusters out in disk order

In reply to Youngjun Park

The clusters set aside for the image come off the free list in the order they were freed, so on a device that has been in use the image is written back and forth across it.

Sort them by cluster index once they are set aside, so the image is written front to back. A rotating disk then writes it and reads it back without seeking back and forth.

Assisted-by Claude:claude-opus-5
---
 mm/swap_hibernate.c | 9 +++++++++
 mm/swapfile.c       | 1 +
 2 files changed, 10 insertions(+)
mm/swap_hibernate.c +9 -0
index 3431443716d4..23dc766f04cd 100644
@@ -13,11 +13,20 @@ static struct swap_info_struct *hib_si;
 /* Free clusters set aside for the image, out of the allocator's reach. */
 static LIST_HEAD(hib_free_clusters);
 
+static int hib_cluster_cmp(void *priv, const struct list_head *a,
+			   const struct list_head *b)
+{
+	return list_entry(a, struct swap_cluster_info, list) <
+	       list_entry(b, struct swap_cluster_info, list) ? -1 : 1;
+}
+
 static void hib_claim_free_clusters(struct swap_info_struct *si)
 {
 	spin_lock(&si->lock);
 	list_splice_tail_init(&si->free_clusters, &hib_free_clusters);
 	spin_unlock(&si->lock);
+
+	list_sort(NULL, &hib_free_clusters, hib_cluster_cmp);
 }
 
 static void hib_return_free_clusters(struct swap_info_struct *si)
mm/swapfile.c +1 -0
index 86ed8c6adae7..98ed75708661 100644
@@ -38,6 +38,7 @@
 #include <linux/swapfile.h>
 #include <linux/export.h>
 #include <linux/sort.h>
+#include <linux/list_sort.h>
 #include <linux/completion.h>
 #include <linux/suspend.h>
 #include <linux/zswap.h>
Signature · 1 line
2.48.1
author reviewer
#7 lore raw

RFC PATCH 7/10 PM: hibernate: build one bio per contiguous run of the image

In reply to Youngjun Park

The image is written with a bio per page, and the block layer merges them back into requests. A 5G image takes about 1.3M bio allocations and nearly as many merges.

One page per bio dates from when the page lock was the completion signal. Commit 343df3c79c62 ("suspend: simplify block I/O handling") removed that constraint, but the bio per page stayed.

Keep filling one bio while the next page follows the last one, and submit it when the run breaks or the bio is full. Size the bio from the device limits. Reads are unchanged.

The compressed path writes the image in rounds and waits for its threads between them. Submit the bio at the end of each round, so the last pages of a round do not wait for the next one.

Without compression, a 5G image now takes about 12.7K bios, and writing it on a VM took 12 to 15% less time.

Assisted-by Claude:claude-fable-5
Assisted-by Claude:claude-opus-5
---
 kernel/power/swap.c | 96 +++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 88 insertions(+), 8 deletions(-)
kernel/power/swap.c +88 -8
index 09c6c50bf837..34e47c662798 100644
@@ -252,25 +252,55 @@ struct hib_bio_batch {
 	wait_queue_head_t	wait;
 	blk_status_t		error;
 	struct blk_plug		plug;
+	struct bio		*cur;		/* write bio still being filled */
+	unsigned int		max_pages;	/* what the device takes at once */
+	unsigned int		nr_pages;	/* what the next bio is sized for */
 };
 
+/* How much of the image the device is willing to take as one request. */
+static unsigned int hib_bio_max_pages(struct block_device *bdev)
+{
+	unsigned int pages = queue_max_bytes(bdev_get_queue(bdev)) >> PAGE_SHIFT;
+
+	pages = min_t(unsigned int, pages, bdev_max_segments(bdev));
+	return bio_max_segs(max(pages, 1U));
+}
+
 static void hib_init_batch(struct hib_bio_batch *hb)
 {
 	atomic_set(&hb->count, 0);
 	init_waitqueue_head(&hb->wait);
 	hb->error = BLK_STS_OK;
+	hb->cur = NULL;
+	hb->max_pages = hib_bio_max_pages(file_bdev(hib_resume_bdev_file));
+	hb->nr_pages = 1;
 	blk_start_plug(&hb->plug);
 }
 
+/* Hand over the bio being filled, if there is one. */
+static void hib_submit_cur(struct hib_bio_batch *hb)
+{
+	struct bio *bio = hb->cur;
+
+	if (!bio)
+		return;
+
+	hb->cur = NULL;
+	atomic_inc(&hb->count);
+	submit_bio(bio);
+}
+
 static void hib_finish_batch(struct hib_bio_batch *hb)
 {
+	hib_submit_cur(hb);
 	blk_finish_plug(&hb->plug);
 }
 
 static void hib_end_io(struct bio *bio)
 {
 	struct hib_bio_batch *hb = bio->bi_private;
-	struct page *page = bio_first_page_all(bio);
+	struct bvec_iter_all iter_all;
+	struct bio_vec *bv;
 
 	if (bio->bi_status) {
 		pr_alert("Read-error on swap-device (%u:%u:%Lu)\n",
@@ -278,11 +308,16 @@ static void hib_end_io(struct bio *bio)
 			 (unsigned long long)bio->bi_iter.bi_sector);
 	}
 
-	if (bio_data_dir(bio) == WRITE)
-		put_page(page);
-	else if (clean_pages_on_read)
-		flush_icache_range((unsigned long)page_address(page),
-				   (unsigned long)page_address(page) + PAGE_SIZE);
+	/* A write bio carries as many pages as the image was contiguous for. */
+	bio_for_each_segment_all(bv, bio, iter_all) {
+		struct page *page = bv->bv_page;
+
+		if (bio_data_dir(bio) == WRITE)
+			put_page(page);
+		else if (clean_pages_on_read)
+			flush_icache_range((unsigned long)page_address(page),
+					   (unsigned long)page_address(page) + PAGE_SIZE);
+	}
 
 	if (bio->bi_status && !hb->error)
 		hb->error = bio->bi_status;
@@ -298,17 +333,53 @@ static int hib_submit_io_sync(blk_opf_t opf, pgoff_t page_off, void *addr)
 			page_off * (PAGE_SIZE >> 9), addr, PAGE_SIZE, opf);
 }
 
+/*
+ * The image goes out a page at a time.  Keep filling one bio for as long as
+ * the next page lands right after the last, so that pages that are
+ * consecutive on the device reach it as one request rather than as many that
+ * the block layer then has to merge.
+ */
 static int hib_submit_io_async(blk_opf_t opf, pgoff_t page_off, void *addr,
 			 struct hib_bio_batch *hb)
 {
+	sector_t sector = page_off * (PAGE_SIZE >> 9);
+	bool write = op_is_write(opf);
 	struct bio *bio;
 
-	bio = bio_alloc(file_bdev(hib_resume_bdev_file), 1, opf,
+	if (write && hb->cur) {
+		bool contiguous = bio_end_sector(hb->cur) == sector;
+
+		if (contiguous && hb->cur->bi_vcnt < hb->cur->bi_max_vecs) {
+			bio_add_virt_nofail(hb->cur, addr, PAGE_SIZE);
+			return 0;
+		}
+
+		/*
+		 * Size the next bio for what the image has just shown.  A run
+		 * that carried on past this bio, or a bio that took more than
+		 * one page, says there are runs here worth the room.  Anything
+		 * else is an image in pieces, and a single page is all it can
+		 * use.
+		 */
+		hb->nr_pages = contiguous || hb->cur->bi_vcnt > 1 ?
+			       hb->max_pages : 1;
+
+		hib_submit_cur(hb);
+	}
+
+	bio = bio_alloc(file_bdev(hib_resume_bdev_file),
+			write ? hb->nr_pages : 1, opf,
 			GFP_NOIO | __GFP_HIGH);
-	bio->bi_iter.bi_sector = page_off * (PAGE_SIZE >> 9);
+	bio->bi_iter.bi_sector = sector;
 	bio_add_virt_nofail(bio, addr, PAGE_SIZE);
 	bio->bi_end_io = hib_end_io;
 	bio->bi_private = hb;
+
+	if (write) {
+		hb->cur = bio;
+		return 0;
+	}
+
 	atomic_inc(&hb->count);
 	submit_bio(bio);
 	return 0;
@@ -316,6 +387,9 @@ static int hib_submit_io_async(blk_opf_t opf, pgoff_t page_off, void *addr,
 
 static int hib_wait_io(struct hib_bio_batch *hb)
 {
+	/* Nothing will complete a bio that was never handed over. */
+	hib_submit_cur(hb);
+
 	/*
 	 * We are relying on the behavior of blk_plug that a thread with
 	 * a plug will flush the plug list before sleeping.
@@ -917,6 +991,12 @@ static int save_compressed_image(struct swap_map_handle *handle,
 			}
 		}
 
+		/*
+		 * Submit the rest of the round now.  Otherwise it can wait in
+		 * the bio until the next round fills it.
+		 */
+		hib_submit_cur(&hb);
+
 		wait_event(crc->done, atomic_read_acquire(&crc->stop));
 		atomic_set(&crc->stop, 0);
 	}
Signature · 1 line
2.48.1
author reviewer
#8 lore raw

RFC PATCH 8/10 PM: hibernate: read the image back a run at a time

In reply to Youngjun Park

The write side now builds one bio per contiguous run. The read side still submits a bio per page.

The image is read in the order it was written, so the offsets arrive in the same runs. Fill the batch for reads the same way as for writes.

The compressed path queues reads ahead and then waits on the reads or on its threads. Submit the queued reads before those waits, so the last of them do not wait for more reads to fill the bio.

Reading a 5G image without compression on a VM took 9 to 10% less time.

Assisted-by Claude:claude-fable-5
Assisted-by Claude:claude-opus-5
---
 kernel/power/swap.c | 33 +++++++++++++++------------------
 1 file changed, 15 insertions(+), 18 deletions(-)
kernel/power/swap.c +15 -18
index 34e47c662798..f2fc8495ddf8 100644
@@ -252,7 +252,7 @@ struct hib_bio_batch {
 	wait_queue_head_t	wait;
 	blk_status_t		error;
 	struct blk_plug		plug;
-	struct bio		*cur;		/* write bio still being filled */
+	struct bio		*cur;		/* the bio still being filled */
 	unsigned int		max_pages;	/* what the device takes at once */
 	unsigned int		nr_pages;	/* what the next bio is sized for */
 };
@@ -308,7 +308,7 @@ static void hib_end_io(struct bio *bio)
 			 (unsigned long long)bio->bi_iter.bi_sector);
 	}
 
-	/* A write bio carries as many pages as the image was contiguous for. */
+	/* A bio carries as many pages as the image was contiguous for. */
 	bio_for_each_segment_all(bv, bio, iter_all) {
 		struct page *page = bv->bv_page;
 
@@ -334,19 +334,18 @@ static int hib_submit_io_sync(blk_opf_t opf, pgoff_t page_off, void *addr)
 }
 
 /*
- * The image goes out a page at a time.  Keep filling one bio for as long as
- * the next page lands right after the last, so that pages that are
- * consecutive on the device reach it as one request rather than as many that
- * the block layer then has to merge.
+ * The image moves a page at a time in either direction.  Keep filling one bio
+ * for as long as the next page lands right after the last, so that pages that
+ * are consecutive on the device reach it as one request rather than as many
+ * that the block layer then has to merge.
  */
 static int hib_submit_io_async(blk_opf_t opf, pgoff_t page_off, void *addr,
 			 struct hib_bio_batch *hb)
 {
 	sector_t sector = page_off * (PAGE_SIZE >> 9);
-	bool write = op_is_write(opf);
 	struct bio *bio;
 
-	if (write && hb->cur) {
+	if (hb->cur) {
 		bool contiguous = bio_end_sector(hb->cur) == sector;
 
 		if (contiguous && hb->cur->bi_vcnt < hb->cur->bi_max_vecs) {
@@ -367,21 +366,13 @@ static int hib_submit_io_async(blk_opf_t opf, pgoff_t page_off, void *addr,
 		hib_submit_cur(hb);
 	}
 
-	bio = bio_alloc(file_bdev(hib_resume_bdev_file),
-			write ? hb->nr_pages : 1, opf,
+	bio = bio_alloc(file_bdev(hib_resume_bdev_file), hb->nr_pages, opf,
 			GFP_NOIO | __GFP_HIGH);
 	bio->bi_iter.bi_sector = sector;
 	bio_add_virt_nofail(bio, addr, PAGE_SIZE);
 	bio->bi_end_io = hib_end_io;
 	bio->bi_private = hb;
-
-	if (write) {
-		hb->cur = bio;
-		return 0;
-	}
-
-	atomic_inc(&hb->count);
-	submit_bio(bio);
+	hb->cur = bio;
 	return 0;
 }
 
@@ -1477,6 +1468,12 @@ static int load_compressed_image(struct swap_map_handle *handle,
 		asked += i;
 		want -= i;
 
+		/*
+		 * Submit the reads queued above now.  Otherwise the last of
+		 * them can wait in the bio until more reads fill it.
+		 */
+		hib_submit_cur(&hb);
+
 		/*
 		 * We are out of data, wait for some more.
 		 */
Signature · 1 line
2.48.1
author reviewer
#9 lore raw

RFC PATCH 9/10 PM: hibernate: tell swap how much space an image needs

In reply to Youngjun Park

Swap has no way to ask how big a hibernation image gets. Add hibernate=reserve[:<size>] and hibernation_reserve_pages(), which swap can call at swapon to learn how much space to hold back.

The answer is <size>, or image_size when no size is given. It is zero without hibernate=reserve and for any area other than the one named by resume=. The image can still outgrow it.

Assisted-by Claude:claude-opus-5
---
 .../admin-guide/kernel-parameters.txt         |  8 ++++++
 include/linux/suspend.h                       |  3 +++
 kernel/power/hibernate.c                      | 26 +++++++++++++++++++
 3 files changed, 37 insertions(+)
Documentation/admin-guide/kernel-parameters.txt +8 -0
index 68647ff4bdd2..eb2454b73f4b 100644
@@ -1980,6 +1980,14 @@ Kernel parameters
 		protect_image	Turn on image protection during restoration
 				(that will set all pages holding image data
 				during restoration read-only).
+		reserve[:<size>]
+				Hold <size> of swap back for the image when the
+				area named by resume= and resume_offset= is
+				swapped on.  Swap cannot use that space.
+				Without <size>, /sys/power/image_size is used.
+				<size> takes K, M or G.  More than half of the
+				area is refused.  Later changes take effect at
+				the next swapon.
 
 	hibernate.compressor= 	[HIBERNATION] Compression algorithm to be
 				used with hibernation.
include/linux/suspend.h +3 -0
index b02876f1ae38..1fb1f5d6ce6a 100644
@@ -393,6 +393,7 @@ extern void hibernation_set_ops(const struct platform_hibernation_ops *ops);
 extern int hibernate(void);
 extern bool system_entering_hibernation(void);
 extern bool hibernation_available(void);
+unsigned long hibernation_reserve_pages(dev_t dev, sector_t offset);
 asmlinkage int swsusp_save(void);
 extern struct pbe *restore_pblist;
 int pfn_is_nosave(unsigned long pfn);
@@ -412,6 +413,8 @@ static inline void hibernation_set_ops(const struct platform_hibernation_ops *op
 static inline int hibernate(void) { return -ENOSYS; }
 static inline bool system_entering_hibernation(void) { return false; }
 static inline bool hibernation_available(void) { return false; }
+static inline unsigned long hibernation_reserve_pages(dev_t dev, sector_t offset)
+{ return 0; }
 
 static inline int hibernate_quiet_exec(int (*func)(void *data), void *data) {
 	return -ENOTSUPP;
kernel/power/hibernate.c +26 -0
index 907d791b85ad..225fe55ee703 100644
@@ -41,6 +41,8 @@
 static int nocompress;
 static int noresume;
 static int nohibernate;
+static int reserve_swap;
+static unsigned long reserve_size;
 static int resume_wait;
 static unsigned int resume_delay;
 static char resume_file[256] = CONFIG_PM_STD_PARTITION;
@@ -113,6 +115,26 @@ bool hibernation_available(void)
 		!secretmem_active() && !cxl_mem_active();
 }
 
+/**
+ * hibernation_reserve_pages - How much swap to hold back for an image.
+ * @dev: the block device the swap area lives on.
+ * @offset: the first block of the swap area on that device.
+ *
+ * Only the area named by resume= and resume_offset= holds anything back.
+ * The image can still grow past this amount.
+ *
+ * Return: the number of pages to hold back, or zero when nothing should be.
+ */
+unsigned long hibernation_reserve_pages(dev_t dev, sector_t offset)
+{
+	if (!reserve_swap || !hibernation_available())
+		return 0;
+	if (!dev || dev != swsusp_resume_device || offset != swsusp_resume_block)
+		return 0;
+
+	return DIV_ROUND_UP(reserve_size ? reserve_size : image_size, PAGE_SIZE);
+}
+
 /**
  * hibernation_set_ops - Set the global hibernate operations.
  * @ops: Hibernation operations to use in subsequent hibernation transitions.
@@ -1437,6 +1459,10 @@ static int __init hibernate_setup(char *str)
 	} else if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)
 		   && !strncmp(str, "protect_image", 13)) {
 		enable_restore_image_protection();
+	} else if (!strncmp(str, "reserve", 7)) {
+		reserve_swap = 1;
+		if (str[7] == ':')
+			reserve_size = memparse(str + 8, NULL);
 	}
 	return 1;
 }
Signature · 1 line
2.48.1
author reviewer
#10 lore raw

RFC PATCH 10/10 mm, swap: hold swap space back for a hibernation image at swapon

In reply to Youngjun Park

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
---
 include/linux/swap.h |  2 ++
 mm/swap_hibernate.c  | 78 ++++++++++++++++++++++++++++++++++++++++----
 mm/swapfile.c        | 23 ++++++++++++-
 3 files changed, 95 insertions(+), 8 deletions(-)
include/linux/swap.h +2 -0
index 94c894c7ad9d..66d59828b327 100644
@@ -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 */
mm/swap_hibernate.c +71 -7

116 lines not on this page · raw message

mm/swapfile.c +22 -1
index 98ed75708661..3b79cec6fd27 100644
@@ -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 {
Signature · 1 line
2.48.1
#11 lore raw
In reply to Youngjun Park
Quoted 12 lines · Youngjun Park

This series improves how hibernation allocates swap slots for its image. It starts from a few observations about what happens while the image is written. With them the allocator can hand the image contiguous runs, the image I/O can be batched per run, and hibernation gets faster. Contiguous I/O pattern is friendly to flash device also.

Slot allocation matters for hibernation speed. After commit 0ff67f990bd4 ("mm, swap: remove swap slot cache") in v6.15, writing the image was about ten times slower on some SSDs [1], until commit 396f57b57200 ("mm, swap: speed up hibernation allocation and writeout") fixed it in v7.1.

Quoted 4 lines

Note
- I was responsible for the observation and design,
while receiving substantial support from LLMs throughout the series.
- Submitting this patch to confirm whether this work is progressing or not.

Hi Youngjun

Thanks for the patch!

Splitting out the hibernation specific part out of swapfile.c looks a good idea to me. We can then compile that file conditionally rather than having a huge ifdef block in swapfile.c. Would be nice to get more feedback from hibnernation side.

Quoted 25 lines

Observations
============

1. While the image is written, swap can only hand out slots that are already free. Swap cache cannot be reclaimed to make more.

folio_swapcache_freeable() refuses every folio while storage is suspended. The image may already hold a folio as clean swap cache. If its slot were freed and reused for the image, the resumed kernel could later drop that folio and read it back from the slot, now with the wrong data.

Storage is suspended before the image is written, so every allocation for the image falls in that window.

hibernate()
freeze_processes() user space is frozen
hibernation_snapshot()
freeze_kernel_threads() kswapd is frozen
hibernate_preallocate_memory() may swap out to shrink memory
pm_restrict_gfp_mask() storage is suspended from here
create_image() the snapshot is taken
swsusp_write() image slots are allocated here
power_down()

...

Quoted 7 lines

2. Image slots are not freed and reused while the image is written. And whatever the allocator changes during the write is gone at resume, because the resumed kernel is the snapshot.

With observation 1, once the image owns a free cluster it can use all of it. Nothing has to be recorded per slot, neither in the swap table nor as a memcg id.

That's pretty nice indeed. But I'm a bit concerned about having a specific cluster isolation path for hibernation.

Will it be cleaner to provide a more generic cluster sized allocation (PMD sized) so common swap can benefit too?

And I'm not sure is IO batching working properly before? How much performance gain is due to the cluster sized IO?

But if other appraoches won't work or hibernation is really special, and we can keep all the hibernation tricky clean and simple in just one place, maybe it's not too bad.

Quoted 24 lines

3. User space and kswapd do not swap out while the image is written, and allocations from the page allocator cannot start swap I/O. What is left is rare. DAMON pageout and the memcg high work can still reach swap(This is all I found. anything else?), and both were seen running in that window.

So the allocator can favor the image then. Other users take slots from the nonfull and frag clusters and leave the free clusters to the image.

With these the allocator gets simpler and faster, and batching the image read and write becomes easy.

What the series does
====================

1-2 fixes, a slot leak after a failed test_resume and a NULL
dereference for a swapfile with no block device (some bug fix)

  3      move the hibernation code to mm/swap_hibernate.c (refactor)
  4      skip swap cache reclaim while storage is suspended (optimization)
  5-6    hand the image whole free clusters, in disk order (exploit contiguous space)
  7-8    write and read the image one bio per run (batch I/O)
  9-10   optional reservation at swapon, hibernate=reserve (assure contiguous space)
Quoted 6 lines

Based on mm-new (383fc05d4650) with patches 2 to 4 of [3] under it. Patch 1 of [3] is in mm-new as 10d9012e83ef.

Note. [3] gives hibernation slots their own swap table entry, keeps readahead off them, and frees them by offset alone. The single slot path of patch 5 builds on that.

Nice, maybe that series need a refresh to get merged first.

Quoted 59 lines

Results
=======

Setup
- qemu, 12G RAM, 4 CPUs, no KVM. Times only compare against each other.
- swap on virtio-blk as a non-rotational device
- image 5.0G, written with hibernate=nocompress
- 3 reps of two hibernations each. Times are medians of the 4 to 6 samples per cell that no host load hit.
- base is patch 3 and allocates as mm-new does, allocator is patch 6, allocator + bio is patch 8

Rows. runs is how many contiguous stretches of the device the image ends up in. bios is how many bios the kernel allocates and submits to write it. In both cases the image fits in free clusters, so the fallback to nonfull and frag clusters is not measured. Percentages are against base.

Shuffled free list. A device that has been in use, emptied.
- 6G swap, 5.4G of 2M tmpfs files swapped out, then all removed in random order
- every cluster is free, the free list is in free order, not in disk order

base allocator allocator + bio

  write, s             10.60     9.32 (-12%)       7.94 (-25%)
  read, s               9.15     8.92  (-3%)       8.10 (-11%)

runs 3560 1 1
bios 1.30M 1.30M 12.7K

Holes in nonfull clusters. What taking free clusters first buys.
- 12G swap, one 4G file swapped out, every other 64K of it freed
- 2G of 64K holes in 2048 clusters, 8G of free clusters
- mm-new fills the holes first, the series takes the free clusters

base allocator allocator + bio

  write, s             10.69    10.07  (-6%)       8.81 (-18%)
  read, s              12.21    10.89 (-11%)       9.78 (-20%)
  runs                 31899               1                 1
  bios                 1.30M           1.30M             12.8K

Summary against base
- the allocator cuts write time by 6 to 12%
- allocator + bio cuts write time by 18 to 25% and read time by

    11 to 20%

These are VM numbers without compression. Compression, the default, and real hardware are still to be checked.

Next steps
==========

Things to keep working on after this RFC. Comments are welcome.

1. Dropping swap cache before hibernation starts, so more slots are free. This series does not do that.

Quoted 8 lines

2. Whether the reservation in patches 9 and 10 is worth keeping. It makes sure the image gets contiguous slots when swap has room to spare.

3. A block device of its own for hibernation instead of swap. Not taken for now. Sharing one device keeps the spare space useful, the existing infrastructure stays, and the ideas above give much the same effect.

So is the idea for 2 and 3 here to make sure hibernation always success by avoid the allocator using too much for common swap?

We only want one of them I think, and we need to be careful here to not make the maintainance messup by adding too many knobs...

Quoted 6 lines

4. Whether the extent tree can go. A normal hibernation never walks it, the swap state comes back as it was at the snapshot. It is only walked to free the slots after an error or a wake from hybrid sleep. With the slots marked in the swap table [3] and taken as whole clusters, a free could find them without it.

The extent tree is not a hibernation issue right? At least for block based swap the extent tree is useless (only one node). I think swap_ops can be used to make this limited to certain swap_ops (e.g. file swap ops).

And maybe, the swap_ops can provide some interface for hibenation usage to make things cleaner?

Quoted 5 lines

5. Whether SNAPSHOT_ALLOC_SWAP_PAGE should refuse a request made before storage is suspended. Such a request gets single slots from the normal allocator today, and s2disk only asks after SNAPSHOT_CREATE_IMAGE anyway.

Is that a even a right thing to do during hibernation?

Quoted 3 lines

6. Two cases are not measured yet. A device with both a shuffled free list and partly used clusters. An image bigger than the free clusters, so part of it comes from nonfull and frag clusters.

I think that's fine, free cluster shuffle should not effect the performance much as 2M is a pretty big IO unit. For the fragmentation batching IO should be very helpful.

#12 lore raw
Quoted 3 lines

Hi Youngjun

Thanks for the patch!

Hi Kairui,

Sorry for the late reply. I was away on a family trip.

Some of your questions and points need more thought, so I'll follow up on those once I've reached a conclusion.

Quoted 4 lines

Splitting out the hibernation specific part out of swapfile.c looks a good idea to me. We can then compile that file conditionally rather than having a huge ifdef block in swapfile.c. Would be nice to get more feedback from hibnernation side.

Ack, I will keep going that way. :)

Quoted 5 lines

That's pretty nice indeed. But I'm a bit concerned about having a specific cluster isolation path for hibernation.

Will it be cleaner to provide a more generic cluster sized allocation (PMD sized) so common swap can benefit too?

I am still thinking about it. For now I am not sure what common swap would gain. The gain would be mostly cleanup, e.g. the allocator would no longer treat an allocation without a folio as hibernation.

Quoted 1 line

And I'm not sure is IO batching working properly before?

It depended on the case! Before this series the image is written with a bio per page, and the block layer merges them back only while the slots are contiguous. They are not always contiguous. Two cases come to mind.

1. The image shares the per-CPU cluster with other swap-outs on that CPU, so their slots can fall in between.
2. When nonfull clusters have holes, the image fills the holes first.

When these happen often, batching does not work well.

And plus, on this RFC I manually collect bio before submit. batch will work well from this patch as I think.

Quoted 1 line

How much performance gain is due to the cluster sized IO?

By cluster sized IO, do you mean the batched I/O of patches 7 and 8? Sometimes it is cluster same sized, small sized and bigger sized(little situation maybe).

If so, allocator + bio cut the write time by 18 to 25% and the read time by 11 to 20% in the cover's tests. The large contiguous I/O is also friendlier to flash as I think

Quoted 3 lines

But if other appraoches won't work or hibernation is really special, and we can keep all the hibernation tricky clean and simple in just one place, maybe it's not too bad.

Since you prefer the common swap code, I will try it in the existing allocation path and compare it with the current swap_hibernate.c approach. I will come back on this with the next series.

Quoted 5 lines
Quoted 3 lines

Note. [3] gives hibernation slots their own swap table entry, keeps readahead off them, and frees them by offset alone. The single slot path of patch 5 builds on that.

Nice, maybe that series need a refresh to get merged first.

Yes, I will refresh and resend it soon. :)

Quoted 9 lines
Quoted 8 lines

2. Whether the reservation in patches 9 and 10 is worth keeping. It makes sure the image gets contiguous slots when swap has room to spare.

3. A block device of its own for hibernation instead of swap. Not taken for now. Sharing one device keeps the spare space useful, the existing infrastructure stays, and the ideas above give much the same effect.

Quoted 5 lines

So is the idea for 2 and 3 here to make sure hibernation always success by avoid the allocator using too much for common swap?

We only want one of them I think, and we need to be careful here to not make the maintainance messup by adding too many knobs...

Yes, that's right. Both keep space for the image that normal swap cannot use, so hibernation has room and gets it contiguous. I agree we only want one. 3 looks like too much to me, so I would like comments on 2.

Quoted 9 lines
Quoted 5 lines

4. Whether the extent tree can go. A normal hibernation never walks it, the swap state comes back as it was at the snapshot. It is only walked to free the slots after an error or a wake from hybrid sleep. With the slots marked in the swap table [3] and taken as whole clusters, a free could find them without it.

The extent tree is not a hibernation issue right? At least for block based swap the extent tree is useless (only one node). I think swap_ops can be used to make this limited to certain swap_ops (e.g. file swap ops).

To clarify, item 4 is about hibernation's own tree, swsusp_extents in kernel/power/swap.c, not the swap device's extent tree. It records the image's slots so they can be freed later.

Quoted 2 lines

And maybe, the swap_ops can provide some interface for hibenation usage to make things cleaner?

I need to think more about swap_ops here. Nothing concrete comes to mind yet.

Quoted 6 lines
Quoted 4 lines

5. Whether SNAPSHOT_ALLOC_SWAP_PAGE should refuse a request made before storage is suspended. Such a request gets single slots from the normal allocator today, and s2disk only asks after SNAPSHOT_CREATE_IMAGE anyway.

Is that a even a right thing to do during hibernation?

Sorry, I don't quite get the question (intention). Could you clarify it?

Quoted 7 lines
Quoted 3 lines

6. Two cases are not measured yet. A device with both a shuffled free list and partly used clusters. An image bigger than the free clusters, so part of it comes from nonfull and frag clusters.

I think that's fine, free cluster shuffle should not effect the performance much as 2M is a pretty big IO unit. For the fragmentation batching IO should be very helpful.

You are right. I listed it only because it is the worst case for the long runs this RFC builds. For the I/O, cluster sized runs are enough. Longer runs mainly help the allocator, which can then hand out more slots at once.

Thanks!
Youngjun Park