From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-189.mta1.migadu.com (out-189.mta1.migadu.com [95.215.58.189]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 63D3D3B6370 for ; Tue, 7 Jul 2026 07:33:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.189 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783409603; cv=none; b=KBJnRSOopIawJMceYiz39WX2efRLPT+avryiBIsr1Y7rIyN2qcsk3PLjB5oPR2gHcgn2HnPyi9Yooz7GMq1id2hFum4GtgnERSKaPUgQjGqvSTbaI+Dek3LR5eSKGF+imx7mKas3PbL1McAE9fqNrpcjpYMWaMdJbsbjEN+h7kg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783409603; c=relaxed/simple; bh=/tHSavDniUp+t5gy+zZOP2xdZ6ludl4tJzyoIjnFhWE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-type; b=Ewf1zz6dUHtDu7s2TO2LXKYAc9brpYig+5caJ2OmTsu1Nls/Db/kiE0Uw1WRBIkRzXy5Gl+0e02wRWASLmWuD2Eh4H4RHxTH3jhDXrslMCKmyvNijWMdEyP429esOZ+ShYLu5bWZn5HQOxAGrfi8F68zVb5xd+gk9Y2aABleUBQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=mMueRABV; arc=none smtp.client-ip=95.215.58.189 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="mMueRABV" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1783409599; h=from:from: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; bh=yh6zGJfbd35MyAYRN2MelR3OTuUTkPH69DCSpZ2wP6M=; b=mMueRABVmHdn7VcElUnEDAcYm0NRysqPEOahvsd5x0yRw7EoKJILX/QeALTEPFOvGe2N4G vbUrXA8wyEEXuewdliFtslRd/ARNK3FnZajUFygPl3Mh0uyp3iuIovLR19e0Jo/ZX+PyTk QNUsamfupct0izLjxFxhgqffEZsr6gk= From: Baoquan He 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 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 Message-ID: <20260707073215.72183-4-baoquan.he@linux.dev> In-Reply-To: <20260707073215.72183-1-baoquan.he@linux.dev> References: <20260707073215.72183-1-baoquan.he@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-type: text/plain Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT 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 --- 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