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 97B88606A322 for ; Thu, 3 Dec 2026 18:14:09 +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=tZ3FML5v Received: by kanga.kvack.org (Postfix) id D5F896B05BF; Thu, 3 Dec 2026 13:14:07 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CE8976B05C0; Thu, 3 Dec 2026 13:14:07 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BD8796B05C1; Thu, 3 Dec 2026 13:14:07 -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 8E7D16B05BF for ; Thu, 3 Dec 2026 13:14:07 -0500 (EST) Received: from smtpin08.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 301D5A84B5 for ; Thu, 3 Dec 2026 18:14:07 +0000 (UTC) X-FDA: 85503470934.08.B7F5D3F Received: from mta0.migadu.com (out-7.mta0.migadu.com [91.218.175.7]) by imf09.hostedemail.com (Postfix) with ESMTP id 3A874140003 for ; Thu, 3 Dec 2026 18:14:04 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=tZ3FML5v; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf09.hostedemail.com: domain of baoquan.he@linux.dev designates 91.218.175.7 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=1796321645; b=08v1DloxltR9LophAGQEaiQZTDKETCQhGQLNwJZCW4zeX6/b58SkhKXYGA/YKxorI++Dyo xG3L363FN/DvB7z3JzmMBNs6Eg1IrRz3VWyn1wmZ+mCmSIYVoFSabeCLY8JCJmkybU/eWi MqPMoVoXCyUbbT/B5kQkZ7sCDMZuv9U= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796321645; 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=cfZQhJnM/DU46RJHN34LafSrjwtzMo5uL2P2N2mIOxQ=; b=qwKSn8Ke/RcMMRLnUe1kBJEcluo4Lxl1YUxUwTrHvvKt5N1XkiMEkokFncNo/WiP5rmskQ eqv0Fcd+f4PXMAY2BXWCivBVbXEjqqEeCV0FDMLnqPgQjKdv54cgC2G4QvQ4yLkFrA5PSk W1aSftCfwQGa6V88NTROJ6+mBlr7VyE= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=tZ3FML5v; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf09.hostedemail.com: domain of baoquan.he@linux.dev designates 91.218.175.7 as permitted sender) smtp.mailfrom=baoquan.he@linux.dev X-Envelope-To: linux-mm@kvack.org DKIM-Signature: a=rsa-sha256; bh=0QV2ymvwomFNsAbG6q8dRFPAP7xSOgpgAYjeec91g/w=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796321643; v=1; x=1796926443; b=tZ3FML5vx8PveNfMdqL8vPrtvWHW39Y+B3PwcYkJJ6+yI7HDGzZeEVQB/S6KEzB2LM44IHVM Tkvtb5GLBW7GGhPbsKK8jLMgguru6hJ8BJyQ0Kc+nxKxBNxHcuKm4UCfkPWror55m3V0boWl+bp F7+ORrxCJ6dofADFriZTWxyk= X-Envelope-To: linux-mm@kvack.org Received: by mta12.migadu.com with ESMTPS id afed48b80a77ace4; Thu, 03 Dec 2026 18:14:03 +0000 X-Mizu-Trace-ID: afed48b80a77ace4 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 06/32] mm, swap: use the xswap physical backend Date: Fri, 4 Dec 2026 02:12:53 +0800 Message-ID: <20261203181319.901844-7-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-Rspamd-Queue-Id: 3A874140003 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-Stat-Signature: 1gnqgs9he3qc5d9sxk1trz5eegccnnaa X-Rspamd-Server: rspam07 X-Rspam-User: X-HE-Tag: 1796321644-415266 X-HE-Meta: U2FsdGVkX18X1WQySaOHjN4R8aMBozAkCkZwvNM6npn5yvZgy6XwnAfW+xBg5YHdxSIbalASIllo0JKBcPoNIsrK3is6W3lz0kWE9HTRYf2EiakQ+YBKjyLhPtkNQA81KeHlFK8DHI6tRtImmn+YJNOR2as+Ky8aS7q221BizAKLKb/RPg+oK5Ih/RTJk3QAj+DHJx2f/TXfMQ/0MfiKTrYa874pCfmkk/he4/IphEt/ZI+aHFz55FGrfpurkjuC3cNh6W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m1tuosRFqou7cQ9gxmHr31N1zLwky9SOXxcIlLcpyxVq/AWUDKtPfJC73wukzsfudtYPEA7TEhYSUhrtslutd+HS9e 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 entry that zswap evicts had nowhere to go. Send it to the physical backend, and reserve the backend before decompressing so a failed allocation wastes no work. Now that an xswap entry can be written back, put it on the zswap writeback LRU again. With no real swap device it stays on the LRU and the shrinker rotates it until one appears, instead of staying pinned until swapin or destroy. An incompressible xswap page is stored uncompressed only while a real swap device exists to hold its backend. The zswap shrinker copies a swpentry and drops the LRU lock before verifying it, while xswap maps cluster_info on demand. Check the range under xswap_lock and reject a swpentry outside nr_clusters_mapped. trylock avoids a deadlock: the allocation can trigger reclaim that calls this path again. A slot that was written out has no zswap copy, so swap_read_folio() forwards its read to the physical entry. Signed-off-by: Baoquan He --- mm/page_io.c | 32 ++++++++++++++++++---- mm/zswap.c | 75 ++++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 91 insertions(+), 16 deletions(-) diff --git a/mm/page_io.c b/mm/page_io.c index 2bc3b46e5529..76e8d08961bb 100644 --- a/mm/page_io.c +++ b/mm/page_io.c @@ -481,9 +481,27 @@ static bool swap_read_folio_zeromap(struct folio *folio) void swap_read_folio(struct swap_io_ctx *ctx, struct folio *folio) { struct swap_info_struct *sis = __swap_entry_to_info(folio->swap); - bool synchronous = sis->flags & SWP_SYNCHRONOUS_IO; + swp_entry_t entry = folio->swap; + swp_entry_t phys = {}; + bool synchronous; bool workingset = folio_test_workingset(folio); + /* Resolved here: the batch and submit mode precede zswap_load(). */ + if (unlikely(sis->flags & SWP_XSWAP)) { + struct swap_cluster_info *ci; + unsigned long offset = swp_offset(folio->swap); + + ci = __swap_offset_to_cluster(sis, offset); + if (ci) { + spin_lock(&ci->lock); + phys = xswap_slot_backend(ci, offset % SWAPFILE_CLUSTER); + spin_unlock(&ci->lock); + } + if (phys.val) + entry = phys; + } + synchronous = __swap_entry_to_info(entry)->flags & SWP_SYNCHRONOUS_IO; + VM_BUG_ON_FOLIO(!folio_test_swapcache(folio) && !synchronous, folio); VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); VM_BUG_ON_FOLIO(folio_test_uptodate(folio), folio); @@ -493,7 +511,7 @@ void swap_read_folio(struct swap_io_ctx *ctx, struct folio *folio) * window opens, so that each batch is billed for its own read * rather than for the next folio's lookup and decompression. */ - if (ctx->sio && !swap_can_merge(ctx, folio, folio->swap, READ)) + if (ctx->sio && !swap_can_merge(ctx, folio, entry, READ)) swap_read_submit(ctx); /* @@ -522,8 +540,12 @@ void swap_read_folio(struct swap_io_ctx *ctx, struct folio *folio) if (zswap_load(folio) != -ENOENT) return; - if (unlikely(sis->flags & SWP_XSWAP)) { - /* An xswap entry only lives in zswap: zswap_load() must hit. */ + if (unlikely(sis->flags & SWP_XSWAP) && !phys.val) { + /* + * Neither zswap nor a physical slot has it. No + * folio_mark_uptodate(), so do_swap_page() sees this as a + * failed read and SIGBUSes silently. + */ WARN_ON_ONCE(1); folio_unlock(folio); return; @@ -531,7 +553,7 @@ void swap_read_folio(struct swap_io_ctx *ctx, struct folio *folio) /* We have to read from slower devices. Increase zswap protection. */ zswap_folio_swapin(folio); - swap_add_folio(ctx, folio, folio->swap, READ); + swap_add_folio(ctx, folio, entry, READ); /* * Synchronous devices do the whole read at submission time, submit diff --git a/mm/zswap.c b/mm/zswap.c index b6a328ab787a..188f763a8af9 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -935,9 +935,10 @@ static bool zswap_compress(struct folio *folio, long index, * save the content as is without a compression, to keep the LRU order * of writebacks. If writeback is disabled, reject the page since it * only adds metadata overhead. swap_writeout() will put the page back - * to the active LRU list in the case. An xswap entry in this phase - * is never put on the LRU, so an incompressible page stored for it - * could never be reclaimed: reject it the same way. + * to the active LRU list in the case. An xswap entry can be written + * back only while a real swap device exists to hold its backend; + * with none, an incompressible page stored for it could never be + * reclaimed, so reject it the same way. */ if (comp_ret) { if (!can_writeback) @@ -1091,6 +1092,8 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx, struct folio *folio; struct mempolicy *mpol; struct swap_info_struct *si; + swp_entry_t phys = {}; + bool is_xswap; int ret = 0; /* try to allocate swap cache folio */ @@ -1098,14 +1101,33 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx, if (IS_ERR_OR_NULL(si)) return -ENOENT; - if (si->flags & SWP_XSWAP) { - put_swap_device(si); - return -EINVAL; + is_xswap = !!(si->flags & SWP_XSWAP); + + /* + * The allocation can enter reclaim and grow this device, which takes + * the lock again, so do not hold it across. Check that the entry's + * range is mapped before the allocation. + */ +#ifdef CONFIG_XSWAP + if (is_xswap) { + if (!mutex_trylock(&si->xswap_lock)) { + put_swap_device(si); + return -EAGAIN; + } + if (swp_offset(swpentry) / SWAPFILE_CLUSTER >= + READ_ONCE(si->nr_clusters_mapped)) { + mutex_unlock(&si->xswap_lock); + put_swap_device(si); + return -ENOENT; + } + mutex_unlock(&si->xswap_lock); } +#endif mpol = get_task_policy(current); folio = __swap_cache_alloc_folio(swpentry, GFP_KERNEL, BIT(0), NULL, mpol, NO_INTERLEAVE_INDEX); + put_swap_device(si); /* @@ -1135,6 +1157,15 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx, goto err; } + /* Reserve the destination before dropping the zswap copy. */ + if (is_xswap) { + phys = xswap_backend_alloc(swpentry); + if (!phys.val) { + ret = -ENOMEM; + goto err; + } + } + if (!zswap_decompress(entry, folio)) { ret = -EIO; goto err; @@ -1167,11 +1198,13 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx, folio_put(folio); /* start writeback */ - __swap_writeout(ctx, folio, folio->swap); + __swap_writeout(ctx, folio, is_xswap ? phys : folio->swap); return 0; err: + if (is_xswap && phys.val) + xswap_backend_free(swpentry, phys); swap_cache_del_folio(folio); folio_unlock(folio); folio_put(folio); @@ -1207,10 +1240,12 @@ static enum lru_status shrink_memcg_cb(struct list_head *item, struct list_lru_o void *arg) { struct zswap_entry *entry = container_of(item, struct zswap_entry, lru); + struct swap_info_struct *si; struct zswap_shrink_ctl *ctl = arg; swp_entry_t swpentry; enum lru_status ret = LRU_REMOVED_RETRY; int writeback_result; + bool no_backend; /* * Second chance algorithm: if the entry has its referenced bit set, give it @@ -1256,6 +1291,21 @@ static enum lru_status shrink_memcg_cb(struct list_head *item, struct list_lru_o */ swpentry = entry->swpentry; + /* + * An xswap entry with no real swap device has nowhere to be + * written back to, so keep it on the LRU for when one appears. + * LRU_ROTATE needs the lru lock and the get/put_swap_device() + * pair does not sleep, so this stays ahead of the unlock. + */ + si = get_swap_device(swpentry); + if (IS_ERR_OR_NULL(si)) + return LRU_ROTATE; + no_backend = (si->flags & SWP_XSWAP) && + !atomic_read(&nr_real_swapfiles); + put_swap_device(si); + if (no_backend) + return LRU_ROTATE; + /* * It's safe to drop the lock here because we return either * LRU_REMOVED_RETRY, LRU_RETRY or LRU_STOP. @@ -1555,8 +1605,12 @@ static bool zswap_store_page(struct folio *folio, long index, return false; } - /* In this phase xswap has no device to write the page back to. */ - can_writeback = !(__swap_entry_to_info(page_swpentry)->flags & SWP_XSWAP); + /* + * An xswap entry can be written back only while a real swap device + * exists to hold its physical backend. + */ + can_writeback = !(__swap_entry_to_info(page_swpentry)->flags & SWP_XSWAP) || + atomic_read(&nr_real_swapfiles); if (!zswap_compress(folio, index, entry, pool, can_writeback)) goto compress_failed; @@ -1617,8 +1671,7 @@ static bool zswap_store_page(struct folio *folio, long index, entry->objcg = objcg; entry->referenced = true; INIT_LIST_HEAD(&entry->lru); - if (can_writeback) - zswap_lru_add(entry); + zswap_lru_add(entry); return true; -- 2.54.0