From: Baoquan He <baoquan.he@linux.dev>
To: linux-mm@kvack.org
Cc: akpm@linux-foundation.org, hannes@cmpxchg.org, yosry@kernel.org,
nphamcs@gmail.com, chengming.zhou@linux.dev, chrisl@kernel.org,
kasong@tencent.com, shikemeng@huaweicloud.com, baohua@kernel.org,
youngjun.park@lge.com, linux-kernel@vger.kernel.org,
Baoquan He <baoquan.he@linux.dev>
Subject: [RFC PATCH 3/7] mm/swap_state: do zswap conversion in __swap_cache_do_del_folio
Date: Tue, 7 Jul 2026 15:32:00 +0800 [thread overview]
Message-ID: <20260707073215.72183-4-baoquan.he@linux.dev> (raw)
In-Reply-To: <20260707073215.72183-1-baoquan.he@linux.dev>
Add zswap_try_convert_to_pointer(), called from __swap_cache_do_del_folio
under cluster lock. When a folio is evicted from swap cache and a Shadow
entry is written, this helper checks if zswap has a compressed copy of
the slot and replaces the Shadow with a Pointer entry.
Signed-off-by: Baoquan He <baoquan.he@linux.dev>
---
mm/swap.h | 5 +++++
mm/swap_state.c | 7 +++++++
mm/zswap.c | 35 +++++++++++++++++++++++++++++++++++
3 files changed, 47 insertions(+)
diff --git a/mm/swap.h b/mm/swap.h
index afe2200bd160..1b07a08e5277 100644
--- a/mm/swap.h
+++ b/mm/swap.h
@@ -211,6 +211,8 @@ 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);
#ifdef CONFIG_ZSWAP
+void zswap_try_convert_to_pointer(struct swap_cluster_info *ci,
+ unsigned int ci_off, int type, pgoff_t offset);
unsigned char zswap_swp_tb_get_count(unsigned long swp_tb);
unsigned char zswap_swp_tb_get_flags(unsigned long swp_tb);
int zswap_swp_tb_dup_count(unsigned long swp_tb,
@@ -218,6 +220,9 @@ int zswap_swp_tb_dup_count(unsigned long swp_tb,
void zswap_swp_tb_put_count(unsigned long swp_tb,
struct swap_cluster_info *ci, unsigned int ci_off);
#else
+static inline void zswap_try_convert_to_pointer(struct swap_cluster_info *ci,
+ unsigned int ci_off, int type,
+ pgoff_t offset) {}
static inline unsigned char zswap_swp_tb_get_count(unsigned long swp_tb)
{
return 0;
diff --git a/mm/swap_state.c b/mm/swap_state.c
index 6fd6e3415b71..20fb57adbb7f 100644
--- a/mm/swap_state.c
+++ b/mm/swap_state.c
@@ -278,6 +278,13 @@ static void __swap_cache_do_del_folio(struct swap_cluster_info *ci,
/* If shadow is NULL, we set an empty shadow. */
__swap_table_set(ci, ci_off, shadow_to_swp_tb(shadow,
__swp_tb_get_flags(old_tb)));
+ /*
+ * If zswap has a compressed copy of this slot, convert the
+ * just-written Shadow to a Pointer entry referencing the
+ * zswap_entry, so the data can be found via the swap table.
+ */
+ zswap_try_convert_to_pointer(ci, ci_off, si->type,
+ swp_offset(entry) + ci_off - ci_start);
} while (++ci_off < ci_end);
folio->swap.val = 0;
diff --git a/mm/zswap.c b/mm/zswap.c
index 81115ca4d10a..f6e8259ab56a 100644
--- a/mm/zswap.c
+++ b/mm/zswap.c
@@ -1652,6 +1652,41 @@ void zswap_invalidate(swp_entry_t swp)
zswap_entry_free(entry);
}
+/*
+ * This is called after the Shadow entry has been written. We replace the
+ * just-written Shadow with a Pointer entry pointing to the zswap_entry.
+ *
+ * Context: cluster lock must be held. Cannot sleep.
+ */
+void zswap_try_convert_to_pointer(struct swap_cluster_info *ci,
+ unsigned int ci_off, int type, pgoff_t offset)
+{
+ struct xarray *tree;
+ struct zswap_entry *entry;
+ unsigned long swp_tb;
+
+ tree = &zswap_trees[type][offset >> ZSWAP_ADDRESS_SPACE_SHIFT];
+ if (xa_empty(tree))
+ return;
+
+ entry = xa_load(tree, offset);
+ if (!entry)
+ return;
+
+ swp_tb = __swap_table_get(ci, ci_off);
+ /* Entry must be a Shadow (just written by __swap_cache_do_del_folio) */
+ if (!swp_tb_is_countable(swp_tb))
+ return;
+
+ /*
+ * Save the complete original swap table entry value, which includes
+ * the swap count, zero flag, and working set shadow information.
+ */
+ entry->swp_tb_val = swp_tb;
+
+ __swap_table_set(ci, ci_off, pointer_to_swp_tb(entry));
+}
+
/*
* Read the swap count from a Pointer-type swap table entry. The count
* and flags are stored in zswap_entry->swp_tb_val which preserves the
--
2.54.0
next prev parent reply other threads:[~2026-07-07 7:33 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-07 7:31 [RFC PATCH 0/7] mm: store zswap entries in swap table (Pointer entry) Baoquan He
2026-07-07 7:31 ` [RFC PATCH 1/7] mm/swap_table: add Pointer type support for zswap entry storage Baoquan He
2026-07-07 7:31 ` [RFC PATCH 2/7] mm/zswap: add swp_tb_val to zswap_entry and Pointer accessor helpers Baoquan He
2026-07-07 7:32 ` Baoquan He [this message]
2026-07-07 7:32 ` [RFC PATCH 4/7] mm/zswap, mm/swap_state: handle Pointer entries in add_folio path Baoquan He
2026-07-07 7:32 ` [RFC PATCH 5/7] mm/swapfile: handle Pointer entries in count/dup/put paths Baoquan He
2026-07-07 7:32 ` [RFC PATCH 6/7] mm/zswap, mm/swap_state: migrate zswap to use swap_table for entry indexing Baoquan He
2026-07-07 7:32 ` [RFC PATCH 7/7] mm/zswap, mm/swap_state: replace xarray indexing with swap table lookups Baoquan He
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260707073215.72183-4-baoquan.he@linux.dev \
--to=baoquan.he@linux.dev \
--cc=akpm@linux-foundation.org \
--cc=baohua@kernel.org \
--cc=chengming.zhou@linux.dev \
--cc=chrisl@kernel.org \
--cc=hannes@cmpxchg.org \
--cc=kasong@tencent.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=nphamcs@gmail.com \
--cc=shikemeng@huaweicloud.com \
--cc=yosry@kernel.org \
--cc=youngjun.park@lge.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox