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 49638606A322 for ; Thu, 3 Dec 2026 18:16:46 +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=Q/JMb2RI Received: by kanga.kvack.org (Postfix) id 39FF66B05E2; Thu, 3 Dec 2026 13:16:36 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 351496B05E3; Thu, 3 Dec 2026 13:16:36 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 219216B05E4; Thu, 3 Dec 2026 13:16:36 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id E71C76B05E2 for ; Thu, 3 Dec 2026 13:16:35 -0500 (EST) Received: from smtpin22.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 65E9C1A06AF for ; Thu, 3 Dec 2026 18:16:35 +0000 (UTC) X-FDA: 85503477150.22.A7A4BB8 Received: from mta1.migadu.com (out-181.mta1.migadu.com [95.215.58.181]) by imf21.hostedemail.com (Postfix) with ESMTP id 9BB8D1C0010 for ; Thu, 3 Dec 2026 18:16:32 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="Q/JMb2RI"; spf=pass (imf21.hostedemail.com: domain of baoquan.he@linux.dev designates 95.215.58.181 as permitted sender) smtp.mailfrom=baoquan.he@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796321792; b=cZceyvBR+NugYQKeWwdJEOaTRg7udFrVGsyRyV23ORDzaC/oCtWdj8U1gH6GEZAj0NeH6P aUPzfyVteV2ZTLbZ9dA5zRKxjDXvki2GNUxtXdvh18p42iSqEdF7MtuZltOwShNRaacH6r e2hfv8yNo4u2XZ+Aigk76vM8ILPZw1k= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796321792; 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=pDo7JtiiKCLpL/sKxmwVQeGbNoXC+HWjmDiDFUjtXDo=; b=3DjcxZhgje/mmL8daC5vgxuemMIc+Jl2mBHDN3GQjsnXhyivKWnRTWCdbKCYThxleU+dhJ g9ig3TABZE0sNbA2AlwBu4vvRaWzm9iKODKHvbQFxA99B3ggpdp7NTKBV9ZGu9TL5OUJ8v /C+Iu7CvMOdPQ0BezRKAAhPIy4OmZcE= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="Q/JMb2RI"; spf=pass (imf21.hostedemail.com: domain of baoquan.he@linux.dev designates 95.215.58.181 as permitted sender) smtp.mailfrom=baoquan.he@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-Envelope-To: linux-mm@kvack.org DKIM-Signature: a=rsa-sha256; bh=2iLeAQCUlVewu45W91yLzMSl261DGP5QM1uOJEkkgr0=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796321791; v=1; x=1796926591; b=Q/JMb2RI4CpOpSzChVbON/K+0J8Bvg4zX5gZ0g3p4L+x3qd2sw7G3A6lCkAN5CqmmH/VLQpA KDjA6M3hNxDfBhdj3mxfWClaQXgQBsPhZ8/dC8Jhy45Fs92Smn5aldmPD81RO8lTxzpYQNtSyqj UPQ9S1x19VPbO9zwW2onW1vo= X-Envelope-To: linux-mm@kvack.org Received: by mta12.migadu.com with ESMTPS id fcbd0a893eaf8bd0; Thu, 03 Dec 2026 18:16:31 +0000 X-Mizu-Trace-ID: fcbd0a893eaf8bd0 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 29/32] mm, swap: add accessors for the backend record Date: Fri, 4 Dec 2026 02:13:16 +0800 Message-ID: <20261203181319.901844-30-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: 9BB8D1C0010 X-Stat-Signature: hwo7tfs54gdz9t5fxfyerjz64biy9fud X-Rspam-User: 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: rspam06 X-HE-Tag: 1796321792-69128 X-HE-Meta: U2FsdGVkX18syL6G1KlaE4B//pvlUvYGQ4qH2pdl46HQ2PClq3q7vvTSy9z0mV486SqTXi9JZMErvY4oq5F4w9cJm/6gbUst5SaBSBAl/dTejBX5x270QF5hm5jHdbUfZ+3/k+m5lgr/HBvhJu7iWAQ5mYgIpuyakez1+MbthkCWudKgD2oIlAYRtt3ArgFhDx3hl2/KoahjR0VX5adynE3aLtLZ50oxi4399T7i7CZq75sKhSRJ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viSuOXjifORVOE3c75zSHYYIyHQPQ5ayUYRNFXpmoaNcPCP3Tv7N5AnOM3yYFMbkzZCvIJEiwkGTpD60wBBVWbfMfvFj/r7snR 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 Add swap_backend_get(), swap_backend_set(), swap_backend_erase() and swap_backend_present() to read and update a swap device's per-slot backend record, plus the zswap entry encoding. The next patch wires zswap to them. get, erase and present read without the cluster lock: the table is allocated before the first record is published and freed only when the cluster is empty, so an in-use slot can always reach it. Updates are serialized by the folio lock (store, load, writeback) or the cluster lock (slot free). Only set takes the cluster lock, only to allocate, and it avoids direct reclaim because the caller's folio is locked. present() walks a range within one cluster and returns early without a table; a range that would cross the cluster is refused after a warning. Setting the same handle is a no-op; only a different handle is reported as displaced. Signed-off-by: Baoquan He --- mm/swap.h | 5 ++ mm/swapfile.c | 208 +++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 170 insertions(+), 43 deletions(-) diff --git a/mm/swap.h b/mm/swap.h index cdb82504269b..9b2d8d556bd0 100644 --- a/mm/swap.h +++ b/mm/swap.h @@ -226,6 +226,11 @@ static inline void swap_cluster_unlock_irq(struct swap_cluster_info *ci) extern int swap_retry_table_alloc(swp_entry_t entry, gfp_t gfp); +void *swap_backend_get(swp_entry_t entry); +void *swap_backend_set(swp_entry_t entry, void *handle); +void *swap_backend_erase(swp_entry_t entry); +bool swap_backend_present(swp_entry_t entry, unsigned int nr); + /* * Below are the core routines for doing swap for a folio. * All helpers requires the folio to be locked, and a locked folio diff --git a/mm/swapfile.c b/mm/swapfile.c index 96019307ce75..1862f0b93c92 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -90,6 +90,19 @@ static inline swp_entry_t swap_backend_decode_phys(unsigned long val) return (swp_entry_t){ .val = val >> 1 }; } +static inline unsigned long swap_backend_encode_zswap(void *handle) +{ + unsigned long val = (unsigned long)handle; + + VM_WARN_ON_ONCE(val & 1UL); + return val | 1UL; +} + +static inline void *swap_backend_decode_zswap(unsigned long val) +{ + return (void *)(val & ~1UL); +} + static inline bool swap_backend_is_zswap(unsigned long val) { return val & 1UL; @@ -4731,9 +4744,151 @@ static unsigned long read_swap_header(struct swap_info_struct *si, } /* - * Drop the table of a cluster that is going away. A recorded backend is - * given back first, or its physical slot and reverse mapping would leak. - * Caller holds ci->lock. + * Per-slot backend record of a cluster, allocated on demand and freed + * with the cluster. + */ +static unsigned long *swap_backend_table_locked(struct swap_cluster_info *ci, + gfp_t gfp) +{ + unsigned long *tbl = ci->backend_table; + + if (tbl) + return tbl; + + /* The owning entry is pinned, so the cluster survives the relock. */ + if (gfp & __GFP_DIRECT_RECLAIM) { + unsigned int noreclaim_flags; + + spin_unlock(&ci->lock); + /* + * The folio being stored is locked. Do not recurse into + * reclaim, which could wait for that folio. + */ + noreclaim_flags = memalloc_noreclaim_save(); + tbl = kcalloc(SWAPFILE_CLUSTER, sizeof(*tbl), gfp); + memalloc_noreclaim_restore(noreclaim_flags); + spin_lock(&ci->lock); + } else { + tbl = kcalloc(SWAPFILE_CLUSTER, sizeof(*tbl), gfp); + } + + if (!tbl) + return NULL; + if (cmpxchg(&ci->backend_table, NULL, tbl)) { + kfree(tbl); + tbl = ci->backend_table; + } + return tbl; +} + +/* + * The zswap side of the table. + * + * The table pointer and its slots are read without taking the cluster lock: + * the table is allocated before the first entry is published and freed only + * once the cluster has no in-use slot left, so a caller that holds an + * in-use slot can always reach it. Per-slot updates are serialized by the + * folio lock (store, load and writeback all run with the folio locked) or + * by the cluster lock (the slot free path runs under it). Only set takes + * the cluster lock, and only to allocate the table. + */ +void *swap_backend_get(swp_entry_t swp) +{ + struct swap_cluster_info *ci = __swap_entry_to_cluster(swp); + unsigned long *tbl = READ_ONCE(ci->backend_table); + unsigned long val; + + if (!tbl) + return NULL; + + val = READ_ONCE(tbl[swp_cluster_offset(swp)]); + return swap_backend_is_zswap(val) ? swap_backend_decode_zswap(val) : NULL; +} + +/* Is any slot in [swp, swp + nr) in zswap? The range sits in one cluster. */ +bool swap_backend_present(swp_entry_t swp, unsigned int nr) +{ + struct swap_cluster_info *ci = __swap_entry_to_cluster(swp); + unsigned long *tbl = READ_ONCE(ci->backend_table); + unsigned int slot = swp_cluster_offset(swp); + unsigned int i; + + /* The range is aligned to its own size and never straddles a cluster. */ + if (slot + nr > SWAPFILE_CLUSTER) { + VM_WARN_ON_ONCE(1); + return false; + } + + if (!tbl) + return false; + + for (i = 0; i < nr; i++) { + if (swap_backend_is_zswap(READ_ONCE(tbl[slot + i]))) + return true; + } + return false; +} + +void *swap_backend_set(swp_entry_t swp, void *handle) +{ + struct swap_info_struct *si = __swap_entry_to_info(swp); + struct swap_cluster_info *ci = __swap_entry_to_cluster(swp); + unsigned int slot = swp_cluster_offset(swp); + unsigned long *tbl, old, encoded; + + tbl = READ_ONCE(ci->backend_table); + if (!tbl) { + ci = swap_cluster_lock(si, swp_offset(swp)); + tbl = swap_backend_table_locked(ci, GFP_KERNEL | __GFP_HIGH | + __GFP_NOMEMALLOC); + swap_cluster_unlock(ci); + if (!tbl) + return ERR_PTR(-ENOMEM); + } + + encoded = swap_backend_encode_zswap(handle); + old = READ_ONCE(tbl[slot]); + /* Re-setting the same handle is a no-op, not a displaced entry. */ + if (old == encoded) + return NULL; + + /* + * A folio written back and then redirtied while still in the swap + * cache is swapped out again into the same slot. The physical copy + * is stale then, and zswap takes the slot over. + */ +#ifdef CONFIG_XSWAP + if (old && !swap_backend_is_zswap(old)) + xswap_backend_free(swp, swap_backend_decode_phys(old), 0); +#endif + + WRITE_ONCE(tbl[slot], encoded); + + return swap_backend_is_zswap(old) ? swap_backend_decode_zswap(old) : NULL; +} + +void *swap_backend_erase(swp_entry_t swp) +{ + struct swap_cluster_info *ci = __swap_entry_to_cluster(swp); + unsigned long *tbl = READ_ONCE(ci->backend_table); + unsigned int slot = swp_cluster_offset(swp); + unsigned long val; + + if (!tbl) + return NULL; + + val = READ_ONCE(tbl[slot]); + if (!swap_backend_is_zswap(val)) + return NULL; + + WRITE_ONCE(tbl[slot], 0); + return swap_backend_decode_zswap(val); +} + +/* + * Drop the table of a cluster that is going away. An untagged value is an + * xswap physical backend that has to be given back; a tagged one would be a + * leak, the slot free path invalidates those first. Caller holds ci->lock. */ static void swap_backend_table_free(struct swap_info_struct *si, struct swap_cluster_info *ci) @@ -4848,56 +5003,23 @@ int xswap_check_backing(struct swap_cluster_info *ci, unsigned int off, return i; } -/* Cluster lock held. The mapped prefix only grows while the device exists. */ -static unsigned long *xswap_backend_table_locked(struct swap_cluster_info *ci, - gfp_t gfp) -{ - unsigned long *xs = ci->backend_table; - - if (xs) - return xs; - - /* The owning entry is pinned, so the cluster survives the relock. */ - if (gfp & __GFP_DIRECT_RECLAIM) { - unsigned int noreclaim_flags; - - spin_unlock(&ci->lock); - /* - * The folio being stored is locked. Do not recurse into - * reclaim, which could wait for that folio. - */ - noreclaim_flags = memalloc_noreclaim_save(); - xs = kcalloc(SWAPFILE_CLUSTER, sizeof(*xs), gfp); - memalloc_noreclaim_restore(noreclaim_flags); - spin_lock(&ci->lock); - } else { - xs = kcalloc(SWAPFILE_CLUSTER, sizeof(*xs), gfp); - } - - if (!xs) - return NULL; - if (cmpxchg(&ci->backend_table, NULL, xs)) { - kfree(xs); - xs = ci->backend_table; - } - return xs; -} - /* * 4K per cluster, and this runs in reclaim, so it has to be able to sleep. */ static bool xswap_slot_set_backend(struct swap_cluster_info *ci, unsigned int slot, swp_entry_t phys) { - unsigned long *xs = xswap_backend_table_locked(ci, GFP_KERNEL | __GFP_HIGH | - __GFP_NOMEMALLOC); + unsigned long *tbl = swap_backend_table_locked(ci, GFP_KERNEL | + __GFP_HIGH | + __GFP_NOMEMALLOC); - if (!xs) + if (!tbl) return false; - WRITE_ONCE(xs[slot], swap_backend_encode_phys(phys)); + WRITE_ONCE(tbl[slot], swap_backend_encode_phys(phys)); return true; } + /* A range of 1 << @order slots, no folio attached. */ static unsigned long xswap_alloc_slot_local(struct swap_info_struct *si, unsigned int order) @@ -5052,7 +5174,7 @@ swp_entry_t xswap_backend_alloc(swp_entry_t entry, unsigned int order, ci = swap_cluster_lock(si, off); if (!ci) return (swp_entry_t){}; - if (!xswap_backend_table_locked(ci, GFP_KERNEL | __GFP_HIGH | __GFP_NOMEMALLOC)) { + if (!swap_backend_table_locked(ci, GFP_KERNEL | __GFP_HIGH | __GFP_NOMEMALLOC)) { swap_cluster_unlock(ci); return (swp_entry_t){}; } -- 2.54.0