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 E21A9606A32F for ; Thu, 3 Dec 2026 18:13:58 +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=pJo3oy4R Received: by kanga.kvack.org (Postfix) id 178206B05BC; Thu, 3 Dec 2026 13:13:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 128D46B05BD; Thu, 3 Dec 2026 13:13:58 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0174C6B05BE; Thu, 3 Dec 2026 13:13:57 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id A2F676B05BC for ; Thu, 3 Dec 2026 13:13:57 -0500 (EST) Received: from smtpin20.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 3C9251C3C32 for ; Thu, 3 Dec 2026 18:13:57 +0000 (UTC) X-FDA: 85503470514.20.0919B89 Received: from mta1.migadu.com (out-63.mta1.migadu.com [95.215.58.63]) by imf07.hostedemail.com (Postfix) with ESMTP id 2AB5F4000E for ; Thu, 3 Dec 2026 18:13:54 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=pJo3oy4R; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf07.hostedemail.com: domain of baoquan.he@linux.dev designates 95.215.58.63 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=1796321635; b=i6xJdVbNXtw3Nt7wqzXXx6GZmgBX+0YlK4RjyQnDTQQQ7S8HeUfz8nLH3f5VSizJFI8mBE 15IDxmBM+NdNDM5hRcna3EByosywHTUpkGkM1UwtdI7yhtPzG8WenS6X3dENwlvPyvkpMZ NGO4o+Q15e/RLe2SwaRmriIOGcL7hYs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796321635; 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=a2xmE6GtVE/GROd/4N0GVy1G8cPSSpmqVr1+6gZVDHU=; b=tAmfAY1OIMKm/7syb66AKUc9EBNEp1VAVpiTg9beaHi1rjUyfWg2mjbAFOO23NdTJuq7V2 BZh1dJJuTvsZ2hEr9vvjqpUSwZteJIEgP+8k+yNjAI8FuMpdlwHOeUcy3fFKlQvt3LGhUP g2TQLGOA5d22LWzsEUKCM+liCes+ATg= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=pJo3oy4R; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf07.hostedemail.com: domain of baoquan.he@linux.dev designates 95.215.58.63 as permitted sender) smtp.mailfrom=baoquan.he@linux.dev X-Envelope-To: linux-mm@kvack.org DKIM-Signature: a=rsa-sha256; bh=g9KHoDRbC9jz2VzwjmgA+Tmfj7PpYFGd66+tsv3Odzo=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796321633; v=1; x=1796926433; b=pJo3oy4R4L/ZgGOzimmXuiFFZcK1zgtRvDRZ8BYYdiriA/m3Ygt+V6TEtiPMvJcHrDKOccL2 X+Q4lW+XUSqmhtmLEJAICWZIwHH1g8oDtC/NKDL1jMUEh71g2rkS7kMdpHsa8Odqwns4UimSC2G H7LyberB78ZGEV67+shSzydc= X-Envelope-To: linux-mm@kvack.org Received: by mta11.migadu.com with ESMTPS id bee0369188d9e58b; Thu, 03 Dec 2026 18:13:53 +0000 X-Mizu-Trace-ID: bee0369188d9e58b 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 04/32] mm, swap: add a physical backend for xswap slots Date: Fri, 4 Dec 2026 02:12:51 +0800 Message-ID: <20261203181319.901844-5-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: mkh6zpyenm3d8j91otoknu74h5a7y1u1 X-Rspam-User: X-Rspamd-Queue-Id: 2AB5F4000E X-HE-Tag: 1796321634-457288 X-HE-Meta: U2FsdGVkX1+aa+DDtst2UCWw0TJmIP2DOyf1CoaiC7nd9IuP5Mkesw69X+86v8504zFSLrAFbhn3YyqofB1GSQIQITk6BC8fB9liQSFicPew2rYJeaCrBcl0erMD4VbA/RPsj+VTqphEocCJx0LgvWH8ARopGjGjsfcNVkbQxmOOoOXWeUBV4TjhWeWtnvyQg6eU+M5dMsRV0Ydj5O/r4+sWEpR19GtEeLYdxQvT3o8rsjG5XGzHwQL5IioWemrWDwZBj8uAEuNXJLR2gNsIfUf76Va/McDl9HHC/aTWKH0SD5VWjAEQKtVtq1Qw3TsiHZWsSepvh9/HulaPjmNoMhr4yu4qMArJgGJLhWf9oRBR7MBYY2RVgRLl13JpWtVjZnpTEjClkNR7JTGBzTCVrwTi9ZCNZ+d1VDjQ9NNynC89PzN9cv3Zd94MNK5jJcCB1vEEtbkCD/OhpLuRr36qjFs5w4g5aLQDsXsMudPQSQC5GjT/t2DAND5y56qX7HeafjLh969+H/zhXs2AqxU6ezj65he2E6uRqkicWcvaQdqw58Ej0nG/sl5sPdNqwC7Yzcwjf0mlZR9YO3wh6bIeG4jTHPCNOkyxFuhlk79LsC/JyzRwd3oOm4Pjnm5ewhVFsoGzUTiIULAKR9s2KjyFtRXX1o0RR/X1gOHEa3RbXsMUJJuAqgcRnMzebvZUaW8PGuiejGPeVEibo00SoeUE6D81CeoKIp/oX5OzH9I7L0qdo8P9Gb6rF+ifbiFtW2DGnKP/VAxR4+olvro+JMk7x9G/FjccrkdkQSggOQ+qVQQFJzAAQa97VsFjjbIs4jnpnCRE3d1SAlQYM/Do/OZGoyqUvs8SeV+T1t0lPUWs6X/B/HBbAyIdPHF49kArFurxpmhaJnFhBhbn00k2gPUMxNQyzGkUntVgHm8gjtdjFBP8NqL2bXgWoVcOoDaIxbfrLRsOVwjiWzJobIWxt3O TxZmZHKT H04tWiBJOGSpVpEOe9Ki25wTfh8qXzAAaSSy5E6jc7c2LRUtCA+sFSY+yLsm1ctGJ1Jz+f26tBQVOvSlDR9vdVmYqfFQb8bStAcFVAXWxqnzPBFuSbJ1FkttTNmUvCZFhFFF03ADE+zbebob0YHj7PQ40mhSJONOSw9+j7uRAnulfBsk7N/QlCTTQyqxTkCGsPn6Vr8QZO0paqGpmBO966r5rNJnwg83ivybRC2mt8QZEKtQ+P+VsugIaHIQ8/6+Je+uxpq8mrJBLUxlOqsExhma3MqujZnstEjXMaDmXoRAh4RL+frQ4BTjUULYmG/prWn+SjJ/JAbyjIIjA+eJ8y41gmVr3iirPuLbsyUE6PrgxkJMNe1KOgTljx4wRhuRDO+kcfzjrVbtQKpR1gyo73m65l61IIYEC8LNecA20JusgsaWzJpspfZHKc5Uokq5MBhfon2rLwClXn/fiLY4Jhjypmok6wc+JMwggRsgqwOnLLRq54tF1JynWBg== 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 slot holds its data only in the zswap pool. There is no second copy anywhere, so once the pool drops the entry the data is gone. Give xswap slots a physical backend they can be written out to: a slot on the highest-priority real swap device. The backend takes its slots from a lower-priority device. The cluster it fills must not go into this CPU's swapout cache: swap_alloc_fast() reuses that cache without checking the device, so one backend allocation would send every later swapout there and xswap would never be picked again. The backend is recorded per slot in ci->xs_table[], an array of SWAPFILE_CLUSTER unsigned longs that is allocated on first use. If a cluster only ever holds zswap-only slots, xs_table[] is never allocated. Zero means "no backend". Freeing an xswap slot releases its physical slot. The release checks that the physical slot still belongs to this xswap entry. If it belongs to another one, the record is stale: that slot was freed and handed out again, and may by then belong to another entry, whose data must not be dropped. A stale record can also name a slot on a device that has been swapped off or reused, or a cluster whose table is already gone, so the release pins the device, rejects a device reused as xswap, and checks the offset and the table before touching either. The target device is revalidated after it is pinned, so a swap_info reused as an xswap device is not picked as its own backend. Signed-off-by: Baoquan He --- mm/swap.h | 24 ++++ mm/swapfile.c | 339 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 361 insertions(+), 2 deletions(-) diff --git a/mm/swap.h b/mm/swap.h index 8979e8a7eb42..73aadd0f2bc8 100644 --- a/mm/swap.h +++ b/mm/swap.h @@ -58,6 +58,9 @@ 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 #ifdef CONFIG_MEMCG struct swap_memcg_table *memcg_table; /* Swap table entries' cgroup record */ #endif @@ -505,4 +508,25 @@ extern const struct swap_ops swap_bdev_ops; int shmem_writeout(struct swap_io_ctx *ctx, struct folio *folio, struct list_head *folio_list); +#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); +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, + unsigned int slot) +{ + return (swp_entry_t){}; +} + +static inline swp_entry_t xswap_backend_alloc(swp_entry_t entry) +{ + return (swp_entry_t){}; +} + +static inline void xswap_backend_free(swp_entry_t entry, swp_entry_t phys) +{ +} +#endif /* CONFIG_XSWAP */ + #endif /* _MM_SWAP_H */ diff --git a/mm/swapfile.c b/mm/swapfile.c index b164a6c50c29..9eb4284e9bc0 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -87,6 +87,13 @@ static void xswap_debugfs_del(struct swap_info_struct *si); static int xswap_map_clusters(struct swap_info_struct *si, unsigned long start_idx, unsigned long nr, bool link_free); +static void xswap_free_phys_slot(struct swap_info_struct *si, + swp_entry_t entry, swp_entry_t phys); +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 int xswap_create(int prio); static int xswap_create_by_name(const char *name, int swap_flags); @@ -827,6 +834,14 @@ 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_cluster_free_table(ci); move_cluster(si, ci, &si->free_clusters, CLUSTER_FLAG_FREE); ci->order = 0; @@ -1085,6 +1100,12 @@ static bool cluster_reclaim_range(struct swap_info_struct *si, spin_unlock(&ci->lock); do { swp_tb = swap_table_get(ci, offset % SWAPFILE_CLUSTER); + /* + * A pointer-tagged slot backs an xswap entry, not a cached + * folio, so it can't be reclaimed here. Reject the range. + */ + if (swp_tb_is_pointer(swp_tb)) + break; if (swp_tb_get_count(swp_tb)) break; if (swp_tb_is_folio(swp_tb)) @@ -1854,6 +1875,14 @@ static void __swap_cluster_put_entry(struct swap_cluster_info *ci, lockdep_assert_held(&ci->lock); swp_tb = __swap_table_get(ci, ci_off); + /* + * A pointer-tagged slot has no count of its own. Putting one would + * corrupt the reverse mapping, so never touch it here. + */ + if (swp_tb_is_pointer(swp_tb)) { + VM_WARN_ON_ONCE(1); + return; + } count = __swp_tb_get_count(swp_tb); VM_WARN_ON_ONCE(count <= 0); @@ -1962,8 +1991,8 @@ static int __swap_cluster_dup_entry(struct swap_cluster_info *ci, lockdep_assert_held(&ci->lock); swp_tb = __swap_table_get(ci, ci_off); - /* Bad or special slots can't be handled */ - if (WARN_ON_ONCE(swp_tb_is_bad(swp_tb))) + /* Bad, pointer-tagged and other special slots can't be handled */ + if (WARN_ON_ONCE(swp_tb_is_bad(swp_tb) || swp_tb_is_pointer(swp_tb))) return -EINVAL; count = __swp_tb_get_count(swp_tb); /* Must be either cached or have a count already */ @@ -2343,6 +2372,11 @@ void __swap_cluster_free_entries(struct swap_info_struct *si, VM_WARN_ON(!swp_tb_is_hibernation(old_tb) && (!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]) + xswap_release_slot_backend(si, ci, ci_off); +#endif + /* Resetting the slot to NULL also clears the inline flags. */ __swap_table_set(ci, ci_off, null_to_swp_tb()); if (!SWAP_TABLE_HAS_ZEROFLAG) @@ -3252,6 +3286,82 @@ static unsigned long find_next_to_unuse(struct swap_info_struct *si, return 0; } + +#ifdef CONFIG_XSWAP +/* + * Free the physical slot @phys and clear its reverse mapping. @entry is the + * xswap entry that recorded @phys. The slot is only freed while it still + * says it belongs to @entry: our record can go stale, and @phys may by then + * have been handed to another xswap entry, whose data must not be dropped. + * + * free_cluster()/partial_free_cluster() require the cluster lock. + */ +static void xswap_free_phys_slot(struct swap_info_struct *si, + swp_entry_t entry, swp_entry_t phys) +{ + struct swap_cluster_info *ci; + unsigned long swp_tb; + unsigned long offset = swp_offset(phys); + + /* The record can be stale: the device may be reused as xswap. */ + if (!get_swap_device_info(si)) + return; + if (si->flags & SWP_XSWAP) + goto out_put; + if (offset >= si->max) + goto out_put; + + ci = swap_cluster_lock(si, offset); + if (!ci) + goto out_put; + + if (!cluster_table_is_alloced(ci)) + goto out_unlock; + + swp_tb = __swap_table_get(ci, offset % SWAPFILE_CLUSTER); + if (!swp_tb_is_pointer(swp_tb) || + xswap_rmap_to_entry(swp_tb).val != entry.val) { + /* Already freed, or the slot was handed to someone else. */ + goto out_unlock; + } + + __swap_table_set(ci, offset % SWAPFILE_CLUSTER, null_to_swp_tb()); + ci->count--; + + /* The slot must not be allocatable before its invalidate hooks run. */ + swap_range_free(si, offset, 1); + + if (!ci->count) + free_cluster(si, ci); + else + partial_free_cluster(si, ci); +out_unlock: + swap_cluster_unlock(ci); +out_put: + put_swap_device(si); +} + +/* + * An xswap slot being freed may still own a physical slot. Drop it, or the + * physical space and its reverse mapping leak. Caller holds the xswap + * cluster lock; the physical cluster lock is only ever taken below it. + */ +static void xswap_release_slot_backend(struct swap_info_struct *si, + struct swap_cluster_info *ci, + unsigned int slot) +{ + swp_entry_t entry = swp_entry(si->type, cluster_offset(si, ci) + slot); + struct swap_info_struct *psi; + swp_entry_t phys = { .val = ci->xs_table[slot] }; + + WRITE_ONCE(ci->xs_table[slot], 0); + + psi = swap_type_to_info(swp_type(phys)); + if (psi) + xswap_free_phys_slot(psi, entry, phys); +} + +#endif /* CONFIG_XSWAP */ static int try_to_unuse(unsigned int type) { struct mm_struct *prev_mm; @@ -3586,6 +3696,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); if (cluster_table_is_alloced(ci)) { swap_cluster_assert_empty(ci, 0, SWAPFILE_CLUSTER, true); swap_cluster_free_table(ci); @@ -4189,6 +4300,230 @@ static void xswap_publish_clusters(struct swap_info_struct *si, spin_unlock(&si->lock); } +/* 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); + + if (!xs || !xs[slot]) + return (swp_entry_t){}; + + return (swp_entry_t){ .val = xs[slot] }; +} + +/* Cluster lock held. The mapped prefix only grows while the device exists. */ +static unsigned long *xswap_xs_table_locked(struct swap_cluster_info *ci, + gfp_t gfp) +{ + unsigned long *xs = ci->xs_table; + + if (xs) + return xs; + + /* The owning entry is pinned, so the cluster survives the relock. */ + if (gfp & __GFP_DIRECT_RECLAIM) { + spin_unlock(&ci->lock); + xs = kcalloc(SWAPFILE_CLUSTER, sizeof(*xs), gfp); + spin_lock(&ci->lock); + } else { + xs = kcalloc(SWAPFILE_CLUSTER, sizeof(*xs), gfp); + } + + if (!xs) + return NULL; + if (cmpxchg(&ci->xs_table, NULL, xs)) { + kfree(xs); + xs = ci->xs_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_xs_table_locked(ci, GFP_KERNEL | __GFP_HIGH | + __GFP_NOMEMALLOC); + + if (!xs) + return false; + WRITE_ONCE(xs[slot], phys.val); + 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; +} + +/* Order-0 slot from @si, no folio attached; goes through the per-CPU cache. */ +static unsigned long xswap_alloc_slot_local(struct swap_info_struct *si) +{ + struct swap_cluster_info *ci; + unsigned long pcp_offset, offset = SWAP_ENTRY_INVALID; + + local_lock(&percpu_swap_cluster.lock); + /* + * A backend slot lands on a device the next swapout would not have + * picked, so it must not become the cached cluster: + * swap_alloc_fast() reuses that cache without picking a device + * again. Start from the cached cursor if it already points at this + * device, but never install this device for the next swapout. + */ + if (this_cpu_read(percpu_swap_cluster.si[0]) == si) { + pcp_offset = this_cpu_read(percpu_swap_cluster.offset[0]); + if (pcp_offset) { + ci = swap_cluster_lock(si, pcp_offset); + if (cluster_is_usable(ci, 0)) + offset = alloc_swap_scan_cluster(si, ci, NULL, + pcp_offset, + true); + else + swap_cluster_unlock(ci); + } + } + if (offset == SWAP_ENTRY_INVALID) + offset = cluster_alloc_swap_entry(si, NULL, true); + local_unlock(&percpu_swap_cluster.lock); + + return offset; +} + +static swp_entry_t xswap_alloc_phys_slot(void) +{ + struct swap_info_struct *devs[MAX_SWAPFILES], *si; + int nr = 0, i; + + /* + * Snapshot the candidate devices in priority order. swap_info + * structs are never freed, so the pointers stay valid; liveness is + * re-checked below. + */ + spin_lock(&swap_avail_lock); + plist_for_each_entry(si, &swap_avail_head, avail_list) { + if (si->flags & SWP_XSWAP) + continue; + if (nr == MAX_SWAPFILES) + break; + devs[nr++] = si; + } + spin_unlock(&swap_avail_lock); + + for (i = 0; i < nr; i++) { + unsigned long offset; + + if (!get_swap_device_info(devs[i])) + continue; + /* The device may have been reused since the snapshot. */ + if (devs[i]->flags & SWP_XSWAP) { + put_swap_device(devs[i]); + continue; + } + offset = xswap_alloc_slot_local(devs[i]); + put_swap_device(devs[i]); + if (offset && offset != SWAP_ENTRY_INVALID) + return swp_entry(devs[i]->type, offset); + } + + return (swp_entry_t){}; +} + +/* Reverse mapping: record @entry as the owner of physical slot @phys. */ +static void xswap_install_rmap(swp_entry_t phys, swp_entry_t entry) +{ + struct swap_info_struct *si = swap_type_to_info(swp_type(phys)); + struct swap_cluster_info *ci; + unsigned long off = swp_offset(phys); + + if (!si) + return; + ci = swap_cluster_lock(si, off); + if (ci) { + __swap_table_set(ci, off % SWAPFILE_CLUSTER, + xswap_entry_to_rmap(entry)); + swap_cluster_unlock(ci); + } +} + +swp_entry_t xswap_backend_alloc(swp_entry_t entry) +{ + struct swap_info_struct *si = __swap_entry_to_info(entry); + struct swap_cluster_info *ci; + unsigned long off = swp_offset(entry); + swp_entry_t phys; + bool recorded = false; + + phys = xswap_alloc_phys_slot(); + if (!phys.val) + return phys; + + /* + * The slot is a shadow until the reverse map is installed below. + * Physical swapoff can see it and retry; the window is short and + * try_to_unuse() reschedules. + */ + xswap_install_rmap(phys, entry); + + ci = swap_cluster_lock(si, off); + if (ci) { + recorded = xswap_slot_set_backend(ci, off % SWAPFILE_CLUSTER, phys); + swap_cluster_unlock(ci); + } + + if (!recorded) { + /* The backend is unrecorded, so the data would be unreachable. */ + xswap_backend_free(entry, phys); + return (swp_entry_t){}; + } + + return phys; +} + +/* Undo xswap_backend_alloc(): the zswap copy is still in place. */ +void xswap_backend_free(swp_entry_t entry, swp_entry_t phys) +{ + struct swap_info_struct *si = __swap_entry_to_info(entry); + struct swap_info_struct *psi; + struct swap_cluster_info *ci; + unsigned long off = swp_offset(entry); + + ci = swap_cluster_lock(si, off); + if (ci) { + unsigned int slot = off % SWAPFILE_CLUSTER; + unsigned long *xs = ci->xs_table; + + if (xs && xs[slot] == phys.val) + WRITE_ONCE(xs[slot], 0); + swap_cluster_unlock(ci); + } + + /* xswap_free_phys_slot() clears the reverse mapping. */ + psi = swap_type_to_info(swp_type(phys)); + if (psi) + xswap_free_phys_slot(psi, entry, phys); +} + static int xswap_map_clusters(struct swap_info_struct *si, unsigned long start_idx, unsigned long nr, bool link_free) -- 2.54.0