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 9A172606A322 for ; Thu, 3 Dec 2026 18:16:39 +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=MsTtq+MN Received: by kanga.kvack.org (Postfix) id D16956B05E1; Thu, 3 Dec 2026 13:16:31 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CEE0F6B05E2; Thu, 3 Dec 2026 13:16:31 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C04596B05E3; Thu, 3 Dec 2026 13:16:31 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 8E5106B05E1 for ; Thu, 3 Dec 2026 13:16:31 -0500 (EST) Received: from smtpin24.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 22B7AC079C for ; Thu, 3 Dec 2026 18:16:31 +0000 (UTC) X-FDA: 85503476982.24.6DA0CC3 Received: from mta0.migadu.com (out-126.mta0.migadu.com [91.218.175.126]) by imf02.hostedemail.com (Postfix) with ESMTP id 1BD298000C for ; Thu, 3 Dec 2026 18:16:28 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=MsTtq+MN; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf02.hostedemail.com: domain of baoquan.he@linux.dev designates 91.218.175.126 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=1796321789; b=l/Mfn8EeJg24M2OFkqH79rC3Fy5EACZ/S6MGbpEtE8w9GXsjIqXmL9RAZw5CocTTFgwy8X 9ZEZOyxxr/iNTS33tG4KS2EFwCqrAHUyqbfFac6usVVCVYeZQkr2kBgZrlhgB7DlDfgsju T+G1+XkWy9DMeAcvz6u/qxr73vwERF0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796321789; 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=7gCm35dBzOTqB+9NL+RsUZ6P9xp9XF1110QV7nPphl4=; b=J18npRoeR+WOi9PNZkWb+oGVzUfose8OQ0yYudPvvscrVVabnY0IBHuv+EEXVYd9GQXSp5 JBfLctZBVMneSamPi41r28zzbA9AFHOp9xr/NmPFRTA4I4Yksf5L1OODDoxynlutD7fqzf QbZ0ia5IDVlfvoOv8AewliwRJNoEDcQ= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=MsTtq+MN; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf02.hostedemail.com: domain of baoquan.he@linux.dev designates 91.218.175.126 as permitted sender) smtp.mailfrom=baoquan.he@linux.dev X-Envelope-To: linux-mm@kvack.org DKIM-Signature: a=rsa-sha256; bh=obBr5ZYCLCfmUcIo6svSTjbQMNv+2Uj0WLobAN2YV2E=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796321787; v=1; x=1796926587; b=MsTtq+MNdY9DjQN+vx/0RY0LVJU5AesTfa7UQ/RIGFxkE3oM5OxACOEPuYRz9IkY6eOd9axv qYxzK0wHaixorHtzRoGjraf3frjHJs6wYuFjAvSe5Vsgb9eNOIGGgzdbqCHvCpgQ3e7AmqciELU sVz1nWnizMyHPhrEQ9B2xxQc= X-Envelope-To: linux-mm@kvack.org Received: by mta10.migadu.com with ESMTPS id a76596600339546a; Thu, 03 Dec 2026 18:16:27 +0000 X-Mizu-Trace-ID: a76596600339546a 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 28/32] mm, swap: encode the per-slot backend record Date: Fri, 4 Dec 2026 02:13:15 +0800 Message-ID: <20261203181319.901844-29-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: xq34zeeckf6gc9yzytd8hpkjuemsb3rx X-Rspam-User: X-Rspamd-Queue-Id: 1BD298000C X-HE-Tag: 1796321788-221161 X-HE-Meta: U2FsdGVkX1/ouOCRvDkKw2CXqqsBqgBMpYbRN8szSXnYCRP36qXDPB63wY21cGGmjfORQNSSlarVePbZpAphFlogmiHJCoRxQcjZeW9i7ZVNetG6wrM1V6Bf18B/RoB+O6lvSVAYhNZd2mccuRNNmA0mla1WeFwzrG5+VbsJB5xBz2N4nFtBV3L0zXYum89aSR0RYNFZKB3lL4aJEAEQ4PI9YV4d+rCzWmzB3mT1o6WksLnOi4wgH8Zm8V6VpUK5/H+PngTeSmq3CNcJ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X5lELA6Thvaryb4bKao7SFwdM6n+HQfeM87HhTwwyBhXEgfguVhvnE16aXmnifdyaDsmgXhk2D 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 A backend record holds either the physical swap entry that backs an xswap slot, or, once zswap records its entries here, a zswap entry. Encode the two so they can share a word: 0 no backend entry << 1 physical backend swap entry ptr | 1 zswap entry pointer A zswap entry comes from a slab cache and is aligned to at least sizeof(void *), so bit 0 is free for the tag. Tagging it in place leaves every other bit of the pointer alone, including any PAC/MTE tag, and works on 32-bit too. The physical entry is shifted, so it needs the top bit of the entry clear; swp_entry() leaves it clear. A word from this table must never be handed to the swp_tb_* helpers; it is only a swp_entry_t after swap_backend_decode_phys(). Only xswap writes the table today, so this is a no-op. Signed-off-by: Baoquan He --- mm/swapfile.c | 94 +++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 77 insertions(+), 17 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 4bf4d4d3f597..96019307ce75 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -63,6 +63,38 @@ /* File-less, so /proc/swaps shows this name; swapoff -a passes it back. */ #define XSWAP_NAME "xswap" +/* + * The backend table holds one word per slot: + * + * 0 - no backend + * entry << 1 - physical backend swap entry (bit 0 clear) + * ptr | 1 - zswap entry pointer (bit 0 set) + * + * A zswap entry comes from a slab cache, so it is aligned to at least + * sizeof(void *): bit 0 is free for the tag. Only that bit is touched: + * the rest of the pointer, including any PAC/MTE tag, is kept as is. + * + * This is not the swap table: a word here must never be handed to the + * swp_tb_* helpers, and the shifted entry is not a swp_entry_t until it + * goes through swap_backend_decode_phys(). + */ +static inline unsigned long swap_backend_encode_phys(swp_entry_t entry) +{ + /* The shift needs the top bit free. swp_entry() leaves it clear. */ + VM_WARN_ON_ONCE(entry.val & (1UL << (BITS_PER_LONG - 1))); + return entry.val << 1; +} + +static inline swp_entry_t swap_backend_decode_phys(unsigned long val) +{ + return (swp_entry_t){ .val = val >> 1 }; +} + +static inline bool swap_backend_is_zswap(unsigned long val) +{ + return val & 1UL; +} + static void swap_backend_table_free(struct swap_info_struct *si, struct swap_cluster_info *ci); @@ -3484,8 +3516,18 @@ 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->backend_table[slot] }; + unsigned long val = READ_ONCE(ci->backend_table[slot]); + swp_entry_t phys; + /* + * Still in zswap: zswap_invalidate() drops the entry once the slot + * is released, and this pointer must not be taken for a physical + * slot here. + */ + if (!val || swap_backend_is_zswap(val)) + return; + + phys = swap_backend_decode_phys(val); WRITE_ONCE(ci->backend_table[slot], 0); psi = swap_type_to_info(swp_type(phys)); @@ -3515,11 +3557,13 @@ 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->backend_table[slot]) }; + unsigned long val = READ_ONCE(ci->backend_table[slot]); unsigned long swp_tb; + swp_entry_t phys; - if (!phys.val) + if (!val || swap_backend_is_zswap(val)) return; /* still in zswap, no backend */ + phys = swap_backend_decode_phys(val); pci = __swap_entry_to_cluster(phys); spin_lock(&pci->lock); swp_tb = __swap_table_get(pci, swp_cluster_offset(phys)); @@ -3543,15 +3587,16 @@ static void xswap_clear_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); - unsigned long xswp_tb, pswp_tb; + unsigned long xswp_tb, pswp_tb, val; swp_entry_t phys; 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->backend_table[slot]); - if (!phys.val) + val = READ_ONCE(ci->backend_table[slot]); + if (!val || swap_backend_is_zswap(val)) continue; + phys = swap_backend_decode_phys(val); pci = __swap_entry_to_cluster(phys); spin_lock(&pci->lock); pswp_tb = __swap_table_get(pci, swp_cluster_offset(phys)); @@ -4702,8 +4747,9 @@ static void swap_backend_table_free(struct swap_info_struct *si, for (slot = 0; slot < SWAPFILE_CLUSTER; slot++) { unsigned long val = READ_ONCE(tbl[slot]); + VM_WARN_ON_ONCE(swap_backend_is_zswap(val)); #ifdef CONFIG_XSWAP - if (val) + if (val && !swap_backend_is_zswap(val)) xswap_release_slot_backend(si, ci, slot); #else VM_WARN_ON_ONCE(val); @@ -4748,12 +4794,17 @@ 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->backend_table); + unsigned long *tbl = READ_ONCE(ci->backend_table); + unsigned long val; + + if (!tbl) + return (swp_entry_t){}; - if (!xs || !xs[slot]) + val = READ_ONCE(tbl[slot]); + if (!val || swap_backend_is_zswap(val)) return (swp_entry_t){}; - return (swp_entry_t){ .val = xs[slot] }; + return swap_backend_decode_phys(val); } /* @@ -4763,6 +4814,7 @@ swp_entry_t xswap_slot_backend(struct swap_cluster_info *ci, unsigned int slot) int xswap_check_backing(struct swap_cluster_info *ci, unsigned int off, unsigned int nr) { + unsigned long val; swp_entry_t first; unsigned int i; @@ -4774,13 +4826,20 @@ int xswap_check_backing(struct swap_cluster_info *ci, unsigned int off, } /* In zswap, which cannot load a large folio. */ - if (!ci->backend_table || !ci->backend_table[off]) + if (!ci->backend_table) + return 1; + val = READ_ONCE(ci->backend_table[off]); + if (!val || swap_backend_is_zswap(val)) return 1; + first = swap_backend_decode_phys(val); - first.val = ci->backend_table[off]; for (i = 1; i < nr; i++) { - swp_entry_t cur = { .val = ci->backend_table[off + i] }; + swp_entry_t cur; + val = READ_ONCE(ci->backend_table[off + i]); + if (!val || swap_backend_is_zswap(val)) + break; + cur = swap_backend_decode_phys(val); if (swp_type(cur) != swp_type(first) || swp_offset(cur) != swp_offset(first) + i) break; @@ -4835,7 +4894,7 @@ static bool xswap_slot_set_backend(struct swap_cluster_info *ci, if (!xs) return false; - WRITE_ONCE(xs[slot], phys.val); + WRITE_ONCE(xs[slot], swap_backend_encode_phys(phys)); return true; } @@ -5078,12 +5137,13 @@ 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->backend_table; + unsigned long *tbl = ci->backend_table; - if (xs && xs[slot] == phys.val + i) { + if (tbl && tbl[slot] == swap_backend_encode_phys( + swp_entry(swp_type(phys), poff + i))) { unsigned short id = xswap_entry_id(ci, slot); - WRITE_ONCE(xs[slot], 0); + WRITE_ONCE(tbl[slot], 0); if (id && !mem_cgroup_swap_memsw_accounted()) mem_cgroup_swap_uncharge(id, 1); } -- 2.54.0