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 26572606A332 for ; Thu, 3 Dec 2026 18:16:33 +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=k4xLjLuJ Received: by kanga.kvack.org (Postfix) id 1F3166B05E0; Thu, 3 Dec 2026 13:16:30 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 17C916B05E2; Thu, 3 Dec 2026 13:16:30 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 055CA6B05E0; Thu, 3 Dec 2026 13:16:29 -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 B402F6B05E0 for ; Thu, 3 Dec 2026 13:16:29 -0500 (EST) Received: from smtpin18.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay08.hostedemail.com (Postfix) with ESMTP id E982C140824 for ; Thu, 3 Dec 2026 18:16:28 +0000 (UTC) X-FDA: 85503476856.18.49DB333 Received: from mta0.migadu.com (out-126.mta0.migadu.com [91.218.175.126]) by imf22.hostedemail.com (Postfix) with ESMTP id E5D70C0003 for ; Thu, 3 Dec 2026 18:16:26 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=k4xLjLuJ; spf=pass (imf22.hostedemail.com: domain of baoquan.he@linux.dev designates 91.218.175.126 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=1796321787; b=pG9Cwvs9OjeNnpduV7XMLJjSO1Ud9BEsOjJ2ljG7VVT/kSWVB1V0oU3NxQ2fAUvELx6zEY k8eOFSWKpIRPjEyhaX/7j/krfc36wsAke+M7x2p2IQt6AjnkzXrvhkZ9tl4Bf7GMpgrzXY qcHfmww+/A3og8f4KodUKvTlqepvsWc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796321787; 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=1irKnjOMFepr4gToU7IYwsvmwZ8GuTcA1rcQIAlWHsU=; b=bNxYo9qNEiF3xsomb/6LpDAIG350lTFedYJadTIu8+I4jhqXnK8wS1/Q70ZgvGNycItT8I l5G4H/U7BJFUQQewXOCnTnbh/+BdciqAw8TBaypwHdbP4EQqODwXxWFRp2fNVTmUCO2nLM /zJJfGPlI+SYirqnglh7GjBX8vKbzqI= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=k4xLjLuJ; spf=pass (imf22.hostedemail.com: domain of baoquan.he@linux.dev designates 91.218.175.126 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=28aX6/0CVmN5DMlhHhNwUXzaWFMT757EEBhq5MWnAEM=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796321784; v=1; x=1796926584; b=k4xLjLuJ6ithj+v8puDjbBNVdz8iPUVih2lRQgTKjRsrgoxKgfUnmDPaWPwR4D46eWOLCZuU f7yHuuCI91nxGPj9ce27FyqWmaaGHMeeU/CJ83xrVomSqkTEtFjmfW4XyfBNfqKM5P4rLuZWwoq IUturWevxmAFjhowuHvzcCms= X-Envelope-To: linux-mm@kvack.org Received: by mta10.migadu.com with ESMTPS id 8978d30700176148; Thu, 03 Dec 2026 18:16:23 +0000 X-Mizu-Trace-ID: 8978d30700176148 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 27/32] mm, swap: keep a per-cluster backend record for every swap device Date: Fri, 4 Dec 2026 02:13:14 +0800 Message-ID: <20261203181319.901844-28-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: E5D70C0003 X-Rspamd-Server: rspam03 X-Stat-Signature: myfbt7gdxggbazreigb4mf11r3o47jm7 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-Rspam-User: X-HE-Tag: 1796321786-911162 X-HE-Meta: U2FsdGVkX19JXfHV32G8+tJ9vCxaUaN1GbFVQLzqNt+BnJN6Vmz1fvby09CpYwUYFm5SIC6JXGY6b3dj5Liq3rXExHaqan1iCTZwvXvaPRC6PgFtNxhZLSblIOq+xPHcxbMAqxNYcj7atxwAs/8cJnP1wD3M0vnIrnCjKdK43BvQKZh7eHw8RVSD2g+gYqSzcJwXERWfoRIOUG9Ftl954trz5fwbGHMynDLeA1DojoJniv25rK2JYtiYP5VEKgu05edN8S/kqR4DlJJ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N4GEwjcsZMfcNrorLYSnTWJxWFN1rrjCezqO6Ssa6cZNUHRkLw3HYIV7TxUiuJVSKw02gLSPulDUv1UaZYFp85s4UUPDWH2qQONVv 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 xswap keeps an xs_table per cluster to record the physical slot backing each xswap slot. Give every swap device that table instead, renamed to backend_table, so a future user does not need a second per-cluster array. The table is allocated on demand and freed with the cluster in __free_cluster() and at teardown; a device that never records a backend pays only the pointer. At teardown, give the backend records back before the swap table that holds the owner id. Allocate the table without entering direct reclaim: the caller's folio is locked, and reclaim could wait for it. Only xswap uses the table for now, so this is a no-op. Signed-off-by: Baoquan He --- mm/swap.h | 4 +- mm/swapfile.c | 112 ++++++++++++++++++++++++++++---------------------- 2 files changed, 63 insertions(+), 53 deletions(-) diff --git a/mm/swap.h b/mm/swap.h index 2cd87a26843a..cdb82504269b 100644 --- a/mm/swap.h +++ b/mm/swap.h @@ -58,9 +58,7 @@ struct swap_cluster_info { u8 order; atomic_long_t __rcu *table; /* Swap table entries, see mm/swap_table.h */ unsigned int *extend_table; /* For large swap count, protected by ci->lock */ -#ifdef CONFIG_XSWAP - unsigned long *xs_table; /* Physical backend per slot, lazily allocated */ -#endif + unsigned long *backend_table; #ifdef CONFIG_MEMCG struct swap_memcg_table *memcg_table; /* Swap table entries' cgroup record */ #endif diff --git a/mm/swapfile.c b/mm/swapfile.c index 2bd573b159ee..4bf4d4d3f597 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -63,6 +63,9 @@ /* File-less, so /proc/swaps shows this name; swapoff -a passes it back. */ #define XSWAP_NAME "xswap" +static void swap_backend_table_free(struct swap_info_struct *si, + struct swap_cluster_info *ci); + #ifdef CONFIG_XSWAP /* * Clusters mapped per grow: one page worth of cluster_info, so the @@ -93,8 +96,6 @@ static void xswap_free_phys_slot(struct swap_info_struct *si, static void xswap_release_slot_backend(struct swap_info_struct *si, struct swap_cluster_info *ci, unsigned int slot); -static void xswap_free_xs_table(struct swap_info_struct *si, - struct swap_cluster_info *ci); static void xswap_mark_cache_only(struct swap_info_struct *si, struct swap_cluster_info *ci, unsigned int slot); @@ -834,14 +835,11 @@ static void swap_cluster_schedule_discard(struct swap_info_struct *si, static void __free_cluster(struct swap_info_struct *si, struct swap_cluster_info *ci) { swap_cluster_assert_empty(ci, 0, SWAPFILE_CLUSTER, false); -#ifdef CONFIG_XSWAP /* * An empty cluster can still hold records. Give them back first: * swap_cluster_free_table() frees the table the owner ID comes from. */ - if (si->flags & SWP_XSWAP) - xswap_free_xs_table(si, ci); -#endif + swap_backend_table_free(si, ci); swap_cluster_free_table(ci); move_cluster(si, ci, &si->free_clusters, CLUSTER_FLAG_FREE); ci->order = 0; @@ -2000,7 +1998,7 @@ static void swap_put_entries_cluster(struct swap_info_struct *si, /* count will be 0 after put, slot can be reclaimed */ need_reclaim = true; #ifdef CONFIG_XSWAP - if (ci->xs_table) + if (ci->backend_table) xswap_mark_cache_only(si, ci, ci_off); #endif } @@ -2110,7 +2108,7 @@ static int swap_dup_entries_cluster(struct swap_info_struct *si, } } while (++ci_off < ci_end); #ifdef CONFIG_XSWAP - if (ci->xs_table) + if (ci->backend_table) xswap_clear_cache_only(si, ci, ci_start, nr); #endif swap_cluster_unlock(ci); @@ -2468,7 +2466,7 @@ void __swap_cluster_free_entries(struct swap_info_struct *si, (!swp_tb_is_shadow(old_tb) || __swp_tb_get_count(old_tb) > 1)); #ifdef CONFIG_XSWAP - if (ci->xs_table && ci->xs_table[ci_off]) + if (ci->backend_table && ci->backend_table[ci_off]) xswap_release_slot_backend(si, ci, ci_off); #endif @@ -3486,9 +3484,9 @@ static void xswap_release_slot_backend(struct swap_info_struct *si, swp_entry_t entry = swp_entry(si->type, cluster_offset(si, ci) + slot); struct swap_info_struct *psi; unsigned short id = xswap_entry_id(ci, slot); - swp_entry_t phys = { .val = ci->xs_table[slot] }; + swp_entry_t phys = { .val = ci->backend_table[slot] }; - WRITE_ONCE(ci->xs_table[slot], 0); + WRITE_ONCE(ci->backend_table[slot], 0); psi = swap_type_to_info(swp_type(phys)); if (psi) @@ -3517,7 +3515,7 @@ static void xswap_mark_cache_only(struct swap_info_struct *si, { struct swap_cluster_info *pci; swp_entry_t xentry = swp_entry(si->type, cluster_offset(si, ci) + slot); - swp_entry_t phys = { .val = READ_ONCE(ci->xs_table[slot]) }; + swp_entry_t phys = { .val = READ_ONCE(ci->backend_table[slot]) }; unsigned long swp_tb; if (!phys.val) @@ -3551,7 +3549,7 @@ static void xswap_clear_cache_only(struct swap_info_struct *si, xswp_tb = __swap_table_get(ci, slot); if (!swp_tb_is_folio(xswp_tb) || swp_tb_get_count(xswp_tb) != 1) continue; - phys.val = READ_ONCE(ci->xs_table[slot]); + phys.val = READ_ONCE(ci->backend_table[slot]); if (!phys.val) continue; pci = __swap_entry_to_cluster(phys); @@ -3743,7 +3741,7 @@ static int xswap_unuse_rmap(struct swap_info_struct *si, unsigned long offset) if (xswap_slot_backend(xci, slot).val == phys.val) { id = xswap_entry_id(xci, slot); - WRITE_ONCE(xci->xs_table[slot], 0); + WRITE_ONCE(xci->backend_table[slot], 0); if (id && !mem_cgroup_swap_memsw_accounted()) mem_cgroup_swap_uncharge(id, 1); } @@ -4113,7 +4111,7 @@ static void free_swap_cluster_info(struct swap_info_struct *si) for (i = 0; i < nr_mapped; i++) { ci = &cluster_info[i]; spin_lock(&ci->lock); - xswap_free_xs_table(si, ci); + swap_backend_table_free(si, ci); if (cluster_table_is_alloced(ci)) { swap_cluster_assert_empty(ci, 0, SWAPFILE_CLUSTER, true); swap_cluster_free_table(ci); @@ -4134,6 +4132,7 @@ static void free_swap_cluster_info(struct swap_info_struct *si) ci = cluster_info + i; /* Cluster with bad marks count will have a remaining table */ spin_lock(&ci->lock); + swap_backend_table_free(si, ci); if (cluster_table_is_alloced(ci)) { swap_cluster_assert_empty(ci, 0, SWAPFILE_CLUSTER, true); swap_cluster_free_table(ci); @@ -4686,6 +4685,35 @@ static unsigned long read_swap_header(struct swap_info_struct *si, return maxpages; } +/* + * 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. + */ +static void swap_backend_table_free(struct swap_info_struct *si, + struct swap_cluster_info *ci) +{ + unsigned long *tbl = ci->backend_table; + unsigned int slot; + + if (!tbl) + return; + + for (slot = 0; slot < SWAPFILE_CLUSTER; slot++) { + unsigned long val = READ_ONCE(tbl[slot]); + +#ifdef CONFIG_XSWAP + if (val) + xswap_release_slot_backend(si, ci, slot); +#else + VM_WARN_ON_ONCE(val); +#endif + } + + WRITE_ONCE(ci->backend_table, NULL); + kfree(tbl); +} + #ifdef CONFIG_XSWAP /* * Link the new clusters and publish the larger range in one si->lock @@ -4720,7 +4748,7 @@ static void xswap_publish_clusters(struct swap_info_struct *si, /* The physical slot backing @slot, or zero. */ swp_entry_t xswap_slot_backend(struct swap_cluster_info *ci, unsigned int slot) { - unsigned long *xs = READ_ONCE(ci->xs_table); + unsigned long *xs = READ_ONCE(ci->backend_table); if (!xs || !xs[slot]) return (swp_entry_t){}; @@ -4746,12 +4774,12 @@ int xswap_check_backing(struct swap_cluster_info *ci, unsigned int off, } /* In zswap, which cannot load a large folio. */ - if (!ci->xs_table || !ci->xs_table[off]) + if (!ci->backend_table || !ci->backend_table[off]) return 1; - first.val = ci->xs_table[off]; + first.val = ci->backend_table[off]; for (i = 1; i < nr; i++) { - swp_entry_t cur = { .val = ci->xs_table[off + i] }; + swp_entry_t cur = { .val = ci->backend_table[off + i] }; if (swp_type(cur) != swp_type(first) || swp_offset(cur) != swp_offset(first) + i) @@ -4762,18 +4790,26 @@ int xswap_check_backing(struct swap_cluster_info *ci, unsigned int off, } /* Cluster lock held. The mapped prefix only grows while the device exists. */ -static unsigned long *xswap_xs_table_locked(struct swap_cluster_info *ci, +static unsigned long *xswap_backend_table_locked(struct swap_cluster_info *ci, gfp_t gfp) { - unsigned long *xs = ci->xs_table; + 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); @@ -4781,9 +4817,9 @@ static unsigned long *xswap_xs_table_locked(struct swap_cluster_info *ci, if (!xs) return NULL; - if (cmpxchg(&ci->xs_table, NULL, xs)) { + if (cmpxchg(&ci->backend_table, NULL, xs)) { kfree(xs); - xs = ci->xs_table; + xs = ci->backend_table; } return xs; } @@ -4794,7 +4830,7 @@ static unsigned long *xswap_xs_table_locked(struct swap_cluster_info *ci, static bool xswap_slot_set_backend(struct swap_cluster_info *ci, unsigned int slot, swp_entry_t phys) { - unsigned long *xs = xswap_xs_table_locked(ci, GFP_KERNEL | __GFP_HIGH | + unsigned long *xs = xswap_backend_table_locked(ci, GFP_KERNEL | __GFP_HIGH | __GFP_NOMEMALLOC); if (!xs) @@ -4803,30 +4839,6 @@ static bool xswap_slot_set_backend(struct swap_cluster_info *ci, return true; } -/* - * Drop the table of cluster @idx and every backend it still records. The - * cluster is going away, so nothing else will: the physical slots would stay - * allocated and their reverse mappings would keep pointing at an xswap entry - * whose cluster_info is about to be unmapped. Caller holds ci->lock. - */ -static void xswap_free_xs_table(struct swap_info_struct *si, - struct swap_cluster_info *ci) -{ - unsigned long *xs = ci->xs_table; - unsigned int slot; - - if (!xs) - return; - - for (slot = 0; slot < SWAPFILE_CLUSTER; slot++) { - if (xs[slot]) - xswap_release_slot_backend(si, ci, slot); - } - - kfree(xs); - ci->xs_table = NULL; -} - /* A range of 1 << @order slots, no folio attached. */ static unsigned long xswap_alloc_slot_local(struct swap_info_struct *si, unsigned int order) @@ -4981,7 +4993,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_xs_table_locked(ci, GFP_KERNEL | __GFP_HIGH | __GFP_NOMEMALLOC)) { + if (!xswap_backend_table_locked(ci, GFP_KERNEL | __GFP_HIGH | __GFP_NOMEMALLOC)) { swap_cluster_unlock(ci); return (swp_entry_t){}; } @@ -5066,7 +5078,7 @@ static void __xswap_backend_unlink(swp_entry_t entry, swp_entry_t phys, ci = swap_cluster_lock(si, off + i); if (ci) { unsigned int slot = (off + i) % SWAPFILE_CLUSTER; - unsigned long *xs = ci->xs_table; + unsigned long *xs = ci->backend_table; if (xs && xs[slot] == phys.val + i) { unsigned short id = xswap_entry_id(ci, slot); -- 2.54.0