Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Baoquan He <baoquan.he@linux.dev>
To: linux-mm@kvack.org
Cc: akpm@linux-foundation.org, david@kernel.org, chrisl@kernel.org,
	kasong@tencent.com, hannes@cmpxchg.org, corbet@lwn.net,
	yosry@kernel.org, nphamcs@gmail.com, muchun.song@linux.dev,
	baohua@kernel.org, youngjun.park@lge.com, hughd@google.com,
	baolin.wang@linux.alibaba.com, baoquan.he@linux.dev,
	Baoquan He <hebaoquan@kylinos.cn>
Subject: [PATCH v2 16/32] mm, swap: enable large xswap folios with a contiguous physical slot range
Date: Fri,  4 Dec 2026 02:13:03 +0800	[thread overview]
Message-ID: <20261203181319.901844-17-baoquan.he@linux.dev> (raw)
In-Reply-To: <20261203181319.901844-1-baoquan.he@linux.dev>

From: Baoquan He <hebaoquan@kylinos.cn>

Allow a large order on an xswap device once a block device can back it.
The physical slot range has to be contiguous because the folio is read back with
a single IO from its first physical slot.

If the backend refuses a contiguous slot range, return a split request to
reclaim instead of activating the large folio over and over.  shmem
cannot repair its page cache state on such a return, so it keeps
can_split clear.

When reclaim cannot enter the fs, a REQUIRE_NOFS block device cannot
back the large slot range either.  Carry may_enter_fs into the large-backend
check and into folio_alloc_swap() so the folio is split instead of
allocating a slot range that writeback cannot use.

Signed-off-by: Baoquan He <hebaoquan@kylinos.cn>
---
 include/linux/swap_ops.h |  1 +
 mm/page_io.c             |  7 ++++
 mm/shmem.c               |  2 +-
 mm/swap.h                |  6 ++--
 mm/swapfile.c            | 77 +++++++++++++++++++++++++++++-----------
 mm/vmscan.c              | 44 ++++++++++++++++++++---
 6 files changed, 110 insertions(+), 27 deletions(-)

diff --git a/include/linux/swap_ops.h b/include/linux/swap_ops.h
index 3e3f14e3b98f..3962482fad9d 100644
--- a/include/linux/swap_ops.h
+++ b/include/linux/swap_ops.h
@@ -29,6 +29,7 @@ struct swap_io_ctx {
 	bool			in_thrashing;
 	unsigned long		in_memstall;
 	bool			may_enter_fs;	/* this context may enter the fs */
+	bool			can_split;	/* caller can handle a split folio */
 };
 
 /*
diff --git a/mm/page_io.c b/mm/page_io.c
index 71e442a8f744..080255ac7314 100644
--- a/mm/page_io.c
+++ b/mm/page_io.c
@@ -269,6 +269,13 @@ int swap_writeout(struct swap_io_ctx *ctx, struct folio *folio)
 					   ctx->may_enter_fs);
 		if (!phys.val) {
 			folio_mark_dirty(folio);
+			/*
+			 * No contiguous slot range: split instead of activating a
+			 * large folio over and over.  Only reclaim can take
+			 * the pieces back.
+			 */
+			if (folio_test_large(folio) && ctx->can_split)
+				return SWAP_WRITEOUT_SPLIT;
 			return AOP_WRITEPAGE_ACTIVATE;
 		}
 
diff --git a/mm/shmem.c b/mm/shmem.c
index 344f7edf5e15..f1c4b116c003 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -1954,7 +1954,7 @@ int shmem_writeout(struct swap_io_ctx *ctx, struct folio *folio,
 		folio_mark_uptodate(folio);
 	}
 
-	ret = folio_alloc_swap(folio);
+	ret = folio_alloc_swap(folio, ctx->may_enter_fs);
 	if (!ret) {
 		bool first_swapped = shmem_recalc_inode(inode, 0, nr_pages);
 		int error;
diff --git a/mm/swap.h b/mm/swap.h
index 9739f9d9e9db..1260b0ac6f41 100644
--- a/mm/swap.h
+++ b/mm/swap.h
@@ -247,7 +247,7 @@ extern int swap_retry_table_alloc(swp_entry_t entry, gfp_t gfp);
  *
  * folio_put_swap(): does the opposite thing of folio_dup_swap().
  */
-int folio_alloc_swap(struct folio *folio);
+int folio_alloc_swap(struct folio *folio, bool may_enter_fs);
 int folio_dup_swap(struct folio *folio, struct page *page);
 void folio_put_swap(struct folio *folio, struct page *page);
 
@@ -258,6 +258,8 @@ extern void __swap_cluster_free_entries(struct swap_info_struct *si,
 
 /* linux/mm/page_io.c */
 int sio_pool_init(void);
+/* swap_writeout() wants a large folio split; reclaim only. */
+#define SWAP_WRITEOUT_SPLIT	0x80002
 void swap_read_folio(struct swap_io_ctx *ctx, struct folio *folio);
 void swap_read_submit(struct swap_io_ctx *ctx);
 void swap_write_submit(struct swap_io_ctx *ctx);
@@ -386,7 +388,7 @@ static inline struct swap_info_struct *__swap_entry_to_info(swp_entry_t entry)
 	return NULL;
 }
 
-static inline int folio_alloc_swap(struct folio *folio)
+static inline int folio_alloc_swap(struct folio *folio, bool may_enter_fs)
 {
 	return -EINVAL;
 }
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 6bc14f5d1618..f1257ae98685 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -1372,13 +1372,35 @@ static void swap_reclaim_work(struct work_struct *work)
 	swap_reclaim_full_clusters(si, true);
 }
 
+#ifdef CONFIG_XSWAP
+/* Whether a block device that can back a large xswap folio is available. */
+static bool xswap_has_large_backend(bool may_enter_fs)
+{
+	struct swap_info_struct *si;
+	bool found = false;
+
+	spin_lock(&swap_avail_lock);
+	plist_for_each_entry(si, &swap_avail_head, avail_list) {
+		if ((si->flags & SWP_XSWAP) || !(si->flags & SWP_BLKDEV))
+			continue;
+		if (!may_enter_fs && (si->ops->flags & SWAP_OPS_F_REQUIRE_NOFS))
+			continue;
+		found = true;
+		break;
+	}
+	spin_unlock(&swap_avail_lock);
+
+	return found;
+}
+#endif /* CONFIG_XSWAP */
+
 /*
  * Try to allocate swap entries with specified order and try set a new
  * cluster for current CPU too.
  */
 static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si,
 					      struct folio *folio, unsigned int order,
-					      bool no_cache)
+					      bool may_enter_fs, bool no_cache)
 {
 	struct swap_cluster_info *ci;
 	unsigned long offset = SWAP_ENTRY_INVALID, found = SWAP_ENTRY_INVALID;
@@ -1388,13 +1410,24 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si,
 
 	/*
 	 * Swapfile is not block device so unable
-	 * to allocate large entries.
+	 * to allocate large entries. xswap has no backing file, so it can.
 	 */
-	if (order && !(si->flags & SWP_BLKDEV))
+	if (order && !(si->flags & SWP_BLKDEV) && !(si->flags & SWP_XSWAP))
 		return 0;
 
 #ifdef CONFIG_XSWAP
-	/* Grow before allocating: mapping pages can sleep. */
+	/* A large xswap folio needs a contiguous block device slot range behind it. */
+	if (order && (si->flags & SWP_XSWAP) &&
+	    atomic_read(&nr_real_swapfiles) &&
+	    !xswap_has_large_backend(may_enter_fs))
+		return 0;
+
+	/*
+	 * Top the range up early.  Every path below leaves through `done`,
+	 * so this has to come first; mapping pages can sleep, and doing it
+	 * from the allocation that would otherwise fail leaves nothing to
+	 * fall back on.
+	 */
 	if ((si->flags & SWP_XSWAP) && (si->flags & SWP_WRITEOK) &&
 	    xswap_should_grow(si))
 		xswap_grow(si, false);
@@ -1696,7 +1729,8 @@ static bool get_swap_device_info(struct swap_info_struct *si)
  * Fast path try to get swap entries with specified order from current
  * CPU's swap entry pool (a cluster).
  */
-static bool swap_alloc_fast(struct folio *folio, bool may_zswap)
+static bool swap_alloc_fast(struct folio *folio, bool may_zswap,
+			    bool may_enter_fs)
 {
 	unsigned int order = folio_order(folio);
 	struct swap_cluster_info *ci;
@@ -1731,7 +1765,8 @@ static bool swap_alloc_fast(struct folio *folio, bool may_zswap)
 }
 
 /* Rotate the device and switch to a new cluster */
-static void swap_alloc_slow(struct folio *folio, bool may_zswap)
+static void swap_alloc_slow(struct folio *folio, bool may_zswap,
+			    bool may_enter_fs)
 {
 	struct swap_info_struct *si, *next;
 
@@ -1749,7 +1784,7 @@ static void swap_alloc_slow(struct folio *folio, bool may_zswap)
 		spin_unlock(&swap_avail_lock);
 		if (get_swap_device_info(si)) {
 			cluster_alloc_swap_entry(si, folio, folio_order(folio),
-						 !may_zswap);
+						 may_enter_fs, !may_zswap);
 			put_swap_device(si);
 			if (folio_test_swapcache(folio))
 				return;
@@ -2094,18 +2129,20 @@ static bool folio_may_zswap(struct folio *folio)
 	return may_zswap;
 }
 
-/* Can a non-xswap device still take a slot? */
-static bool real_swap_has_room(void)
+/* Can a non-xswap device take a slot in this context? */
+static bool real_swap_has_room(bool may_enter_fs)
 {
 	struct swap_info_struct *si;
 	bool room = false;
 
 	spin_lock(&swap_avail_lock);
 	plist_for_each_entry(si, &swap_avail_head, avail_list) {
-		if (!(si->flags & SWP_XSWAP)) {
-			room = true;
-			break;
-		}
+		if ((si->flags & SWP_XSWAP) ||
+		    (!may_enter_fs &&
+		     (si->ops->flags & SWAP_OPS_F_REQUIRE_NOFS)))
+			continue;
+		room = true;
+		break;
 	}
 	spin_unlock(&swap_avail_lock);
 	return room;
@@ -2123,7 +2160,7 @@ static bool real_swap_has_room(void)
  * %-ENOSPC if no global swap space is available, or %-ENOMEM if splitting
  * would not help.
  */
-int folio_alloc_swap(struct folio *folio)
+int folio_alloc_swap(struct folio *folio, bool may_enter_fs)
 {
 	unsigned int order = folio_order(folio);
 	unsigned int size = 1 << order;
@@ -2165,10 +2202,10 @@ int folio_alloc_swap(struct folio *folio)
 	 * A folio that cannot zswap can only use a real device; skip the
 	 * scan when none has room.  The failure is still reported below.
 	 */
-	if (may_zswap || real_swap_has_room()) {
+	if (may_zswap || real_swap_has_room(may_enter_fs)) {
 		local_lock(&percpu_swap_cluster.lock);
-		if (!swap_alloc_fast(folio, may_zswap))
-			swap_alloc_slow(folio, may_zswap);
+		if (!swap_alloc_fast(folio, may_zswap, may_enter_fs))
+			swap_alloc_slow(folio, may_zswap, may_enter_fs);
 		local_unlock(&percpu_swap_cluster.lock);
 	}
 
@@ -2212,7 +2249,7 @@ int folio_alloc_swap(struct folio *folio)
 	 * The folio can only use a real device and none has room; splitting
 	 * cannot help.
 	 */
-	if (!may_zswap && !real_swap_has_room())
+	if (!may_zswap && !real_swap_has_room(may_enter_fs))
 		return -ENOMEM;
 
 	return order ? -E2BIG : -ENOMEM;
@@ -2640,7 +2677,7 @@ swp_entry_t swap_alloc_hibernation_slot(int type)
 			swap_cluster_unlock(ci);
 	}
 	if (!offset)
-		offset = cluster_alloc_swap_entry(si, NULL, 0, false);
+		offset = cluster_alloc_swap_entry(si, NULL, 0, true, false);
 	local_unlock(&percpu_swap_cluster.lock);
 	if (offset)
 		entry = swp_entry(si->type, offset);
@@ -4691,7 +4728,7 @@ static unsigned long xswap_alloc_slot_local(struct swap_info_struct *si,
 		}
 	}
 	if (offset == SWAP_ENTRY_INVALID)
-		offset = cluster_alloc_swap_entry(si, NULL, order, true);
+		offset = cluster_alloc_swap_entry(si, NULL, order, true, true);
 	local_unlock(&percpu_swap_cluster.lock);
 
 	return offset;
diff --git a/mm/vmscan.c b/mm/vmscan.c
index c3ba8050ed89..74920609a2b9 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -613,10 +613,46 @@ static pageout_t pageout(struct swap_io_ctx *ctx, struct address_space *mapping,
 	 * or we failed to allocate contiguous swap entries, in which case
 	 * the split out folios get added back to folio_list.
 	 */
-	if (shmem_mapping(mapping))
+	if (shmem_mapping(mapping)) {
+		/*
+		 * shmem_writeout() cannot repair its page cache state on a
+		 * split return.  ctx is reused across folios, so reset it
+		 * every call.
+		 */
+		ctx->can_split = false;
 		res = shmem_writeout(ctx, folio, folio_list);
-	else
+	} else {
+		ctx->can_split = true;
 		res = swap_writeout(ctx, folio);
+	}
+
+	/*
+	 * The backend refused a contiguous range.  Split here; nothing else
+	 * will, and an activated large folio just comes back.
+	 */
+	if (res == SWAP_WRITEOUT_SPLIT) {
+		int order = folio_order(folio);
+
+		/*
+		 * The slots exist but no backend is recorded yet, so the
+		 * split only has to re-point each piece at its own slot.
+		 */
+		if (!split_folio_to_list(folio, folio_list)) {
+#ifdef CONFIG_TRANSPARENT_HUGEPAGE
+			if (order >= HPAGE_PMD_ORDER) {
+				count_memcg_folio_events(folio,
+					THP_SWPOUT_FALLBACK, 1);
+				count_vm_event(THP_SWPOUT_FALLBACK);
+			}
+#endif
+			count_mthp_stat(order, MTHP_STAT_SWPOUT_FALLBACK);
+			/* Pieces are queued; the folio is still locked. */
+			folio_clear_reclaim(folio);
+			return PAGE_ACTIVATE;
+		}
+		folio_mark_dirty(folio);
+		res = AOP_WRITEPAGE_ACTIVATE;
+	}
 
 	if (res < 0)
 		handle_write_error(mapping, folio, res);
@@ -1587,7 +1623,7 @@ static unsigned int shrink_folio_list(struct list_head *folio_list,
 				    split_folio_to_list(folio, folio_list))
 					goto activate_locked;
 			}
-			ret = folio_alloc_swap(folio);
+			ret = folio_alloc_swap(folio, ctx.may_enter_fs);
 			if (ret) {
 				int __maybe_unused order = folio_order(folio);
 
@@ -1606,7 +1642,7 @@ static unsigned int shrink_folio_list(struct list_head *folio_list,
 				}
 #endif
 				count_mthp_stat(order, MTHP_STAT_SWPOUT_FALLBACK);
-				if (folio_alloc_swap(folio))
+				if (folio_alloc_swap(folio, ctx.may_enter_fs))
 					goto activate_locked_split;
 			}
 			/*
-- 
2.54.0



  parent reply	other threads:[~2026-12-03 18:15 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 18:12 [PATCH v2 00/32] mm, swap: extendable swap devices (xswap phases II-IV) Baoquan He
2026-12-03 18:12 ` [PATCH v2 01/32] mm, swap: prepare the swap IO path for xswap backends Baoquan He
2026-12-03 18:12 ` [PATCH v2 02/32] mm, swap: tag a swap table entry with its owning xswap entry Baoquan He
2026-12-03 18:12 ` [PATCH v2 03/32] mm, swap: prepare the folio-less allocation path for xswap Baoquan He
2026-12-03 18:12 ` [PATCH v2 04/32] mm, swap: add a physical backend for xswap slots Baoquan He
2026-12-03 18:12 ` [PATCH v2 05/32] mm, swap: support swapoff of an xswap physical backend Baoquan He
2026-12-03 18:12 ` [PATCH v2 06/32] mm, swap: use the " Baoquan He
2026-12-03 18:12 ` [PATCH v2 07/32] mm, swap: fall back to disk when zswap refuses an xswap page Baoquan He
2026-12-03 18:12 ` [PATCH v2 08/32] mm, swap: reclaim physical slots backing cache-only xswap entries Baoquan He
2026-12-03 18:12 ` [PATCH v2 09/32] mm, swap: drop swap_folio_sector() Baoquan He
2026-12-03 18:12 ` [PATCH v2 10/32] mm, swap: drop a stale xswap physical slot range before reallocating Baoquan He
2026-12-03 18:12 ` [PATCH v2 11/32] mm, swap: publish the xswap backend owner before its reverse map Baoquan He
2026-12-03 18:12 ` [PATCH v2 12/32] mm, swap: skip a NOFS backend for xswap physical writeback Baoquan He
2026-12-03 18:13 ` [PATCH v2 13/32] mm, swap: pass the folio order through the swap slot allocator Baoquan He
2026-12-03 18:13 ` [PATCH v2 14/32] mm, swap: allocate a multi-slot range without a folio Baoquan He
2026-12-03 18:13 ` [PATCH v2 15/32] mm, swap: pass the folio order to the xswap physical backend Baoquan He
2026-12-03 18:13 ` Baoquan He [this message]
2026-12-03 18:13 ` [PATCH v2 17/32] mm, swap: enable THP swapin for xswap entries Baoquan He
2026-12-03 18:13 ` [PATCH v2 18/32] mm, swap: split the swap memcg charge helpers Baoquan He
2026-12-03 18:13 ` [PATCH v2 19/32] mm, swap: do not charge zswap-backed xswap entries Baoquan He
2026-12-03 18:13 ` [PATCH v2 20/32] mm, swap: charge an xswap entry when it gets physical backing Baoquan He
2026-12-03 18:13 ` [PATCH v2 21/32] mm, swap: don't gate xswap on the physical swap free count Baoquan He
2026-12-03 18:13 ` [PATCH v2 22/32] mm, swap: do not retake the cluster lock when uncharging an xswap slot Baoquan He
2026-12-03 18:13 ` [PATCH v2 23/32] mm, swap: uncharge an xswap backend where its record is cleared Baoquan He
2026-12-03 18:13 ` [PATCH v2 24/32] mm, swap: account xswap backends for cgroup v1 Baoquan He
2026-12-03 18:13 ` [PATCH v2 25/32] mm, swap: drop a refused xswap backend slot range directly Baoquan He
2026-12-03 18:13 ` [PATCH v2 26/32] mm, swap: route xswap by the zswap eligibility without flushing Baoquan He
2026-12-03 18:13 ` [PATCH v2 27/32] mm, swap: keep a per-cluster backend record for every swap device Baoquan He
2026-12-03 18:13 ` [PATCH v2 28/32] mm, swap: encode the per-slot backend record Baoquan He
2026-12-03 18:13 ` [PATCH v2 29/32] mm, swap: add accessors for the " Baoquan He
2026-12-03 18:13 ` [PATCH v2 30/32] mm, zswap: index entries in the swap device's backend table Baoquan He
2026-12-03 18:13 ` [PATCH v2 31/32] mm, swap: release a range of backend handles at once Baoquan He
2026-12-03 18:13 ` [PATCH v2 32/32] mm, swap: only run the xswap backend hooks on xswap devices Baoquan He

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=20261203181319.901844-17-baoquan.he@linux.dev \
    --to=baoquan.he@linux.dev \
    --cc=akpm@linux-foundation.org \
    --cc=baohua@kernel.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=chrisl@kernel.org \
    --cc=corbet@lwn.net \
    --cc=david@kernel.org \
    --cc=hannes@cmpxchg.org \
    --cc=hebaoquan@kylinos.cn \
    --cc=hughd@google.com \
    --cc=kasong@tencent.com \
    --cc=linux-mm@kvack.org \
    --cc=muchun.song@linux.dev \
    --cc=nphamcs@gmail.com \
    --cc=yosry@kernel.org \
    --cc=youngjun.park@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