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 12/32] mm, swap: skip a NOFS backend for xswap physical writeback
Date: Fri, 4 Dec 2026 02:12:59 +0800 [thread overview]
Message-ID: <20261203181319.901844-13-baoquan.he@linux.dev> (raw)
In-Reply-To: <20261203181319.901844-1-baoquan.he@linux.dev>
From: Baoquan He <hebaoquan@kylinos.cn>
An xswap folio's swap entry points at the xswap device, but its data
may be written to a real backend. may_enter_fs() reads the device out
of that entry, so it checks the xswap device and not the backend: a
context that cannot enter the fs is still allowed to write, and
xswap_alloc_phys_slot() may pick NFS or SMB. Writing there re-enters
the filesystem.
Pass may_enter_fs down in swap_io_ctx, from sc->gfp_mask and from the
zswap shrinker, and make xswap_alloc_phys_slot() skip a NOFS backend
when it is false. Revalidate the candidate after pinning it, in case
the swap_info was reused as a NOFS device since it was snapshotted.
With none left, AOP_WRITEPAGE_ACTIVATE keeps the folio.
Signed-off-by: Baoquan He <hebaoquan@kylinos.cn>
---
include/linux/swap_ops.h | 1 +
include/linux/zswap.h | 4 ++--
mm/page_io.c | 4 ++--
mm/swap.h | 4 ++--
mm/swapfile.c | 14 +++++++++++---
mm/vmscan.c | 4 +++-
mm/zswap.c | 23 +++++++++++++++--------
7 files changed, 36 insertions(+), 18 deletions(-)
diff --git a/include/linux/swap_ops.h b/include/linux/swap_ops.h
index 58100dfac056..3e3f14e3b98f 100644
--- a/include/linux/swap_ops.h
+++ b/include/linux/swap_ops.h
@@ -28,6 +28,7 @@ struct swap_io_ctx {
bool in_delayacct;
bool in_thrashing;
unsigned long in_memstall;
+ bool may_enter_fs; /* this context may enter the fs */
};
/*
diff --git a/include/linux/zswap.h b/include/linux/zswap.h
index 94746fb71bb6..6d6c791b1167 100644
--- a/include/linux/zswap.h
+++ b/include/linux/zswap.h
@@ -25,7 +25,7 @@ struct zswap_lruvec_state {
};
unsigned long zswap_total_pages(void);
-bool zswap_store(struct folio *folio);
+bool zswap_store(struct folio *folio, bool may_enter_fs);
int zswap_load(struct folio *folio);
void zswap_invalidate(int type, pgoff_t offset, unsigned long nr_entries);
int zswap_swapon(int type, unsigned long nr_pages);
@@ -40,7 +40,7 @@ bool zswap_is_present(swp_entry_t entry, unsigned int nr);
struct zswap_lruvec_state {};
-static inline bool zswap_store(struct folio *folio)
+static inline bool zswap_store(struct folio *folio, bool may_enter_fs)
{
return false;
}
diff --git a/mm/page_io.c b/mm/page_io.c
index 30d46218122b..5aa3717b3d70 100644
--- a/mm/page_io.c
+++ b/mm/page_io.c
@@ -246,7 +246,7 @@ int swap_writeout(struct swap_io_ctx *ctx, struct folio *folio)
*/
swap_zeromap_folio_clear(folio);
- if (zswap_store(folio)) {
+ if (zswap_store(folio, ctx->may_enter_fs)) {
count_mthp_stat(folio_order(folio), MTHP_STAT_ZSWPOUT);
goto out_unlock;
}
@@ -265,7 +265,7 @@ int swap_writeout(struct swap_io_ctx *ctx, struct folio *folio)
swp_entry_t phys;
/* The pool refused it: fall back to a real swap device. */
- phys = xswap_backend_alloc(entry);
+ phys = xswap_backend_alloc(entry, ctx->may_enter_fs);
if (!phys.val) {
folio_mark_dirty(folio);
return AOP_WRITEPAGE_ACTIVATE;
diff --git a/mm/swap.h b/mm/swap.h
index 73aadd0f2bc8..6e3128c54b4f 100644
--- a/mm/swap.h
+++ b/mm/swap.h
@@ -510,7 +510,7 @@ int shmem_writeout(struct swap_io_ctx *ctx, struct folio *folio,
#ifdef CONFIG_XSWAP
swp_entry_t xswap_slot_backend(struct swap_cluster_info *ci, unsigned int slot);
-swp_entry_t xswap_backend_alloc(swp_entry_t entry);
+swp_entry_t xswap_backend_alloc(swp_entry_t entry, bool may_enter_fs);
void xswap_backend_free(swp_entry_t entry, swp_entry_t phys);
#else
static inline swp_entry_t xswap_slot_backend(struct swap_cluster_info *ci,
@@ -519,7 +519,7 @@ static inline swp_entry_t xswap_slot_backend(struct swap_cluster_info *ci,
return (swp_entry_t){};
}
-static inline swp_entry_t xswap_backend_alloc(swp_entry_t entry)
+static inline swp_entry_t xswap_backend_alloc(swp_entry_t entry, bool may_enter_fs)
{
return (swp_entry_t){};
}
diff --git a/mm/swapfile.c b/mm/swapfile.c
index adc7f41cd8a3..7e0bdde39ab0 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -4700,7 +4700,7 @@ static unsigned long xswap_alloc_slot_local(struct swap_info_struct *si)
return offset;
}
-static swp_entry_t xswap_alloc_phys_slot(void)
+static swp_entry_t xswap_alloc_phys_slot(bool may_enter_fs)
{
struct swap_info_struct *devs[MAX_SWAPFILES], *si;
int nr = 0, i;
@@ -4714,6 +4714,9 @@ static swp_entry_t xswap_alloc_phys_slot(void)
plist_for_each_entry(si, &swap_avail_head, avail_list) {
if (si->flags & SWP_XSWAP)
continue;
+ /* Writing there would re-enter the fs this context is in. */
+ if (!may_enter_fs && (si->ops->flags & SWAP_OPS_F_REQUIRE_NOFS))
+ continue;
if (nr == MAX_SWAPFILES)
break;
devs[nr++] = si;
@@ -4730,6 +4733,11 @@ static swp_entry_t xswap_alloc_phys_slot(void)
put_swap_device(devs[i]);
continue;
}
+ if (!may_enter_fs &&
+ (devs[i]->ops->flags & SWAP_OPS_F_REQUIRE_NOFS)) {
+ put_swap_device(devs[i]);
+ continue;
+ }
offset = xswap_alloc_slot_local(devs[i]);
put_swap_device(devs[i]);
if (offset && offset != SWAP_ENTRY_INVALID)
@@ -4756,7 +4764,7 @@ static void xswap_install_rmap(swp_entry_t phys, swp_entry_t entry)
}
}
-swp_entry_t xswap_backend_alloc(swp_entry_t entry)
+swp_entry_t xswap_backend_alloc(swp_entry_t entry, bool may_enter_fs)
{
struct swap_info_struct *si = __swap_entry_to_info(entry);
struct swap_cluster_info *ci;
@@ -4790,7 +4798,7 @@ swp_entry_t xswap_backend_alloc(swp_entry_t entry)
}
swap_cluster_unlock(ci);
- phys = xswap_alloc_phys_slot();
+ phys = xswap_alloc_phys_slot(may_enter_fs);
if (!phys.val)
return phys;
diff --git a/mm/vmscan.c b/mm/vmscan.c
index 443ee5d30531..c3ba8050ed89 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -1369,7 +1369,9 @@ static unsigned int shrink_folio_list(struct list_head *folio_list,
unsigned int nr_reclaimed = 0, nr_demoted = 0;
unsigned int pgactivate = 0;
bool do_demote_pass;
- struct swap_io_ctx ctx = {};
+ struct swap_io_ctx ctx = {
+ .may_enter_fs = !!(sc->gfp_mask & __GFP_FS),
+ };
folio_batch_init(&free_folios);
memset(stat, 0, sizeof(*stat));
diff --git a/mm/zswap.c b/mm/zswap.c
index 188f763a8af9..c367fd04551d 100644
--- a/mm/zswap.c
+++ b/mm/zswap.c
@@ -1066,6 +1066,8 @@ struct zswap_shrink_ctl {
* the swap cache, and set it to true.
*/
bool *encountered_page_in_swapcache;
+ /* Whether the backing allocation may enter the filesystem. */
+ bool may_enter_fs;
};
/*
@@ -1085,7 +1087,7 @@ struct zswap_shrink_ctl {
*/
static int zswap_writeback_entry(struct swap_io_ctx *ctx,
struct zswap_entry *entry,
- swp_entry_t swpentry)
+ swp_entry_t swpentry, bool may_enter_fs)
{
struct xarray *tree;
pgoff_t offset = swp_offset(swpentry);
@@ -1159,7 +1161,7 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx,
/* Reserve the destination before dropping the zswap copy. */
if (is_xswap) {
- phys = xswap_backend_alloc(swpentry);
+ phys = xswap_backend_alloc(swpentry, may_enter_fs);
if (!phys.val) {
ret = -ENOMEM;
goto err;
@@ -1236,6 +1238,7 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx,
* can expect from writeback. We scale down the number of objects available
* for reclaim by this ratio.
*/
+
static enum lru_status shrink_memcg_cb(struct list_head *item, struct list_lru_one *l,
void *arg)
{
@@ -1312,7 +1315,8 @@ static enum lru_status shrink_memcg_cb(struct list_head *item, struct list_lru_o
*/
spin_unlock(&l->lock);
- writeback_result = zswap_writeback_entry(&ctl->io_ctx, entry, swpentry);
+ writeback_result = zswap_writeback_entry(&ctl->io_ctx, entry, swpentry,
+ ctl->may_enter_fs);
if (writeback_result) {
zswap_reject_reclaim_fail++;
@@ -1340,6 +1344,7 @@ static unsigned long zswap_shrinker_scan(struct shrinker *shrinker,
bool encountered_page_in_swapcache = false;
struct zswap_shrink_ctl ctl = {
.encountered_page_in_swapcache = &encountered_page_in_swapcache,
+ .may_enter_fs = !!(sc->gfp_mask & __GFP_FS),
};
unsigned long shrink_ret;
struct blk_plug plug;
@@ -1459,9 +1464,11 @@ static struct shrinker *zswap_alloc_shrinker(void)
* were scanned but none could be written back, or -ENOENT if @memcg has
* writeback disabled, is a zombie cgroup, or has empty zswap LRUs.
*/
-static int shrink_memcg(struct mem_cgroup *memcg)
+static int shrink_memcg(struct mem_cgroup *memcg, bool may_enter_fs)
{
- struct zswap_shrink_ctl ctl = {};
+ struct zswap_shrink_ctl ctl = {
+ .may_enter_fs = may_enter_fs,
+ };
int nid, shrunk = 0, scanned = 0;
struct blk_plug plug;
@@ -1560,7 +1567,7 @@ static void shrink_worker(struct work_struct *w)
goto resched;
}
- ret = shrink_memcg(memcg);
+ ret = shrink_memcg(memcg, true);
/* drop the extra reference */
mem_cgroup_put(memcg);
@@ -1682,7 +1689,7 @@ static bool zswap_store_page(struct folio *folio, long index,
return false;
}
-bool zswap_store(struct folio *folio)
+bool zswap_store(struct folio *folio, bool may_enter_fs)
{
long nr_pages = folio_nr_pages(folio);
swp_entry_t swp = folio->swap;
@@ -1701,7 +1708,7 @@ bool zswap_store(struct folio *folio)
objcg = get_obj_cgroup_from_folio(folio);
if (objcg && !obj_cgroup_may_zswap(objcg)) {
memcg = get_mem_cgroup_from_objcg(objcg);
- if (shrink_memcg(memcg)) {
+ if (shrink_memcg(memcg, may_enter_fs)) {
mem_cgroup_put(memcg);
goto put_objcg;
}
--
2.54.0
next prev parent reply other threads:[~2026-12-03 18:14 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 ` Baoquan He [this message]
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 ` [PATCH v2 16/32] mm, swap: enable large xswap folios with a contiguous physical slot range Baoquan He
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-13-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.