From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 50BB3606A32C for ; Thu, 3 Dec 2026 18:14:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=f3q5r/ZR Received: by kanga.kvack.org (Postfix) id 5E7006B05C9; Thu, 3 Dec 2026 13:14:43 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5972E6B05CA; Thu, 3 Dec 2026 13:14:43 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 485CC6B05CB; Thu, 3 Dec 2026 13:14:43 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id D01616B05C9 for ; Thu, 3 Dec 2026 13:14:42 -0500 (EST) Received: from smtpin21.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 112B114047A for ; Thu, 3 Dec 2026 18:14:42 +0000 (UTC) X-FDA: 85503472404.21.7671EB2 Received: from mta0.migadu.com (out-41.mta0.migadu.com [91.218.175.41]) by imf13.hostedemail.com (Postfix) with ESMTP id 17F992000A for ; Thu, 3 Dec 2026 18:14:39 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="f3q5r/ZR"; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf13.hostedemail.com: domain of baoquan.he@linux.dev designates 91.218.175.41 as permitted sender) smtp.mailfrom=baoquan.he@linux.dev ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796321680; b=B0x6EG3iqPDW3LcvrwqmJ29+37cufoJdeDFYgHMCK4vZR8fbCVP18sIJPRC6fkuCsMkYNL lZ7hTSurNsabgX4xHuFKV3wXMn/kvLL+FLhTQBy8LbVSXBSnRygbQrw+iIY6mVkMrOhSFS M7GTt8IxfvQ/Ui1jcfroHDf/uc+nqy4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796321680; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=VqUjJnuBnOrp0eiHoPkboF38FkGi1FB8IV+j8bQaEKM=; b=V+aYYrHKUEntVWtlD7FbSGkMWy3uMCMjUgUYUObodRMCSoBDcZyL9JsrB/KYfaQSUxIto9 zz7liMJxSFGu2IPUSkmqp2deMhSx9VEOf4gaM1mgVJ304O7Nv9ioM4ftF4NKgqhrKALcfA qjJjtcD2rNlbeQk5Bq19gJOYM8EN7Zc= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="f3q5r/ZR"; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf13.hostedemail.com: domain of baoquan.he@linux.dev designates 91.218.175.41 as permitted sender) smtp.mailfrom=baoquan.he@linux.dev X-Envelope-To: linux-mm@kvack.org DKIM-Signature: a=rsa-sha256; bh=2BzOdPyrERAoLCAvsT7JHYIJNKmofJVtppcOsLQlaFg=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796321678; v=1; x=1796926478; b=f3q5r/ZR6twxM0OxDpSidQDbRlCs2ziFqyxdHeCJnM2An2nqyHy7Zme21B3lxiclVT5dDpmS LF9Kqk+uyvU4HG6ODzgl63BdtHfnjXK6jS20aVPqclPOMB0uO1E69dY+xHh+kQUynnQiOleqybI aJ/UM66CBj0XGS3M25ux49cQ= X-Envelope-To: linux-mm@kvack.org Received: by mta12.migadu.com with ESMTPS id 6051f254ccbecf6e; Thu, 03 Dec 2026 18:14:38 +0000 X-Mizu-Trace-ID: 6051f254ccbecf6e X-Migadu-Flow: FLOW_OUT From: Baoquan He 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 Subject: [PATCH v2 12/32] mm, swap: skip a NOFS backend for xswap physical writeback Date: Fri, 4 Dec 2026 02:12:59 +0800 Message-ID: <20261203181319.901844-13-baoquan.he@linux.dev> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261203181319.901844-1-baoquan.he@linux.dev> References: <20261203181319.901844-1-baoquan.he@linux.dev> X-Archived-By: MIME-Version: 1.0 Content-type: text/plain Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Rspamd-Server: rspam08 X-Stat-Signature: 3gkqx5nn7bu3bu7913h673mjf4c4a6xk X-Rspam-User: X-Rspamd-Queue-Id: 17F992000A X-HE-Tag: 1796321679-712121 X-HE-Meta: U2FsdGVkX18TA+wV2dO5Y+sbLizxl+BqylAM1k6VBADFepQfy72hB+Duqg8JRAUiGCyDH2xxPmerJ1Tkj7IC6aYpQvgW8Y4FxEY/H2PSNBHbZ9X/pX8X7PV4kc78KpYF6w2rK3kYHRo0QD1XZcOXWyp/j8SLSYysfqdLJc8DP2kjdsn7UmN2xPnX8egYi5At1SxjoXJwL2ANBKhLitW2Mr783UEWkdUiFRV1lZIbm/2lRvchO5bKF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gNAaGoHCfN0xf7w9M8TQCO/FyJDmK5w2gTx1sSNlktdMYE/2wCzkh0Mvghd+ugZVSKgFhNMx9LSmhLCApcK23245BbifgkNYi/VCaWxjIh/x Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 From: Baoquan He 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 --- 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