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 8CCAB606A322 for ; Thu, 3 Dec 2026 18:13:49 +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=SHoC/you Received: by kanga.kvack.org (Postfix) id 87BF46B05B6; Thu, 3 Dec 2026 13:13:43 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8055B6B05B8; Thu, 3 Dec 2026 13:13:43 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6F42E6B05B9; Thu, 3 Dec 2026 13:13:43 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id E01706B05B6 for ; Thu, 3 Dec 2026 13:13:42 -0500 (EST) Received: from smtpin08.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 64ADA120810 for ; Thu, 3 Dec 2026 18:13:42 +0000 (UTC) X-FDA: 85503469884.08.D998E1A Received: from mta0.migadu.com (out-239.mta0.migadu.com [91.218.175.239]) by imf12.hostedemail.com (Postfix) with ESMTP id 5CE884000B for ; Thu, 3 Dec 2026 18:13:40 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="SHoC/you"; spf=pass (imf12.hostedemail.com: domain of baoquan.he@linux.dev designates 91.218.175.239 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=1796321620; b=pAGMStTxZFhgtKFR+cS8J4/AMx8GD/RRWdOhZ+FvjQ2qr6kvpg+qFoQl49KbO70mcALXvV hsAdFnwwl2zYDg8BBKthaKljLKJp8dAFPoPg9ocVxFWLM8n0ijyLkJFMCmwuuyBWbgyhll TWqD7j69+z4DvErhIHnFes0X1+Rp6aY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796321620; 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=jzuhm15V7qrzgmBBNlaboFBgRbDegBlxGCpWsCtbaWU=; b=sMBWlAlj3o6DbwhUe19SFHx6ov0RKcPzez2zD5JoJ2I4p7IKRt4TsTvKgUlT+nPIp8Z+ML meTzOaDXIr1dpRdmNi35teDYCFoGz+VVa1OthkMO3trj8gq/rBADYYpuYvt3kWK6Im00z0 Detp/JJTy894XV0B1+XPdxz3H47qLxk= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="SHoC/you"; spf=pass (imf12.hostedemail.com: domain of baoquan.he@linux.dev designates 91.218.175.239 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=m/ix5EUZha7Py1CjQda3PxLoiHdD+a1YGgyoDhipnt8=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796321619; v=1; x=1796926419; b=SHoC/you+8Rzl1QhE6j+0sVlM0yiGOvtbyoRCHUA3PgFkYQ4J6aQJFdrbq93WIaEqdvC011M mGLftFOnPIWZQHWo+C7PltaZzCWLfbg6nZ8mTEnNzhOlNiJK4aexEdCrGaKaAug5eVU6hG4RoGM PSzPX742DCVYuDIEkBv6Vgos= X-Envelope-To: linux-mm@kvack.org Received: by mta10.migadu.com with ESMTPS id 064e01b28296a70c; Thu, 03 Dec 2026 18:13:38 +0000 X-Mizu-Trace-ID: 064e01b28296a70c 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 02/32] mm, swap: tag a swap table entry with its owning xswap entry Date: Fri, 4 Dec 2026 02:12:49 +0800 Message-ID: <20261203181319.901844-3-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-Stat-Signature: jwiatqtsmniudhyz85ccz14c7fcmte13 X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 5CE884000B X-Rspam-User: X-HE-Tag: 1796321620-492568 X-HE-Meta: U2FsdGVkX19IPY2hywv7RCI8zp+Ty71lv0GcxsrNtxeqKMo3ajLOa5KReUGG1mml64UL4X4a/SNvmSL9VeD90rELL68MuKlfLww/7kIIu8vBOsCba0U448+oGSFIw55Y1dssgNaZvB1xGA0cjXpFAa3aMhf9Yc+0moBAGDKNDbL4/+6xuLqW80EoPCXuS4o+gJ/8Nn9fmyZa7AR0ICDhMnOqJikUvX/+k/wYhZ8rokgQaUeYC2nNYMND3kv/U2Vve4fZQRHofagO0c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gi6NJjhfx0yW4NOo+BgXg73pYBc6sBRppeNRQJq/p7Q24fnCOXE4KD4G7ijTZd1HeyXRO91P7SvG2Y/LepOEgeHAOLbSNCDEmYahg== 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 folio can be written out to a physical device. The data then lives in a slot on that device, with no folio in the swap cache. Store the xswap entry in that slot's swap table entry, so the owner of a physical slot can be found again. The low three bits of a swap table entry tell its type; 0b100 is free. Use it to mark an entry that points at its owner, with the xswap type and offset in the bits above. Two paths hand out or cache a slot and know only the other two types. Make them skip a pointer entry: cluster_scan_range() must not hand it out as free, and __swap_cache_add_check() must not put a folio over it. Otherwise readahead on a physical device can walk into it. Co-developed-by: Nhat Pham Signed-off-by: Nhat Pham Signed-off-by: Baoquan He --- mm/swap_state.c | 3 +++ mm/swap_table.h | 61 +++++++++++++++++++++++++++++++++++++++++++++---- mm/swapfile.c | 3 +++ 3 files changed, 63 insertions(+), 4 deletions(-) diff --git a/mm/swap_state.c b/mm/swap_state.c index 90cd99c25dfe..7298a9894737 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -189,6 +189,9 @@ static int __swap_cache_add_check(struct swap_cluster_info *ci, return -ENOENT; ci_off = swp_cluster_offset(targ_entry); old_tb = __swap_table_get(ci, ci_off); + /* Readahead of a physical device can hit an xswap-backing slot. */ + if (swp_tb_is_pointer(old_tb)) + return -ENOENT; if (swp_tb_is_folio(old_tb)) return -EEXIST; /* diff --git a/mm/swap_table.h b/mm/swap_table.h index 072625d60d54..eadd0547d2ad 100644 --- a/mm/swap_table.h +++ b/mm/swap_table.h @@ -28,7 +28,7 @@ struct swap_memcg_table { * NULL: |---------------- 0 ---------------| - Free slot * Shadow: |SWAP_COUNT|Z|---- SHADOW_VAL ---|1| - Swapped out slot * PFN: |SWAP_COUNT|Z|------ PFN -------|10| - Cached slot - * Pointer: |----------- Pointer ----------|100| - (Unused) + * Pointer: |C|-- xswap type/offset --|100| - Backend slot * Bad: |------------- 1 -------------|1000| - Bad slot * HIB: | 0 |------- 1 -------|10000| - Hibernation slot * @@ -50,9 +50,9 @@ struct swap_memcg_table { * - PFN: Swap slot is in use, and cached. Memcg info is recorded on the page * struct. * - * - Pointer: Unused yet. `0b100` is reserved for potential pointer usage - * because only the lower three bits can be used as a marker for 8 bytes - * aligned pointers. + * - Pointer: A physical swap slot backed by an xswap entry. `0b100` marks it + * and the xswap type and offset go above it; C is SWP_RMAP_CACHE_ONLY. See + * the layout in the CONFIG_XSWAP block below. * * - Bad: Swap slot is reserved, protects swap header or holes on swap devices. * @@ -90,6 +90,59 @@ struct swap_memcg_table { /* Hibernation slot: ends with 0b10000, no count, rests of bits are all 1 */ #define SWP_TB_HIB (((~0UL) << 4) & ~SWP_TB_COUNT_MASK) +#ifdef CONFIG_XSWAP +/* + * Pointer-tagged swap table entry: the reverse map from a physical slot to + * the xswap entry it backs. Layout: + * + * Pointer: | xswap_type(8) | xswap_offset(53) |100| + * + * The low three bits identify the entry: 0b000 is a free, bad or hibernation + * slot, a shadow entry ends in 0b01 and a cached folio in 0b10, which leaves + * 0b100 as the only marker still free. + */ +#define SWP_TB_PTR_MARK 0b100UL +#define SWP_TB_PTR_OFF_BITS 53 +#define SWP_TB_PTR_TYPE_BITS 8 +#define SWP_TB_PTR_OFF_SHIFT 3 +#define SWP_TB_PTR_TYPE_SHIFT (SWP_TB_PTR_OFF_SHIFT + \ + SWP_TB_PTR_OFF_BITS) +#define SWP_TB_PTR_OFF_MASK ((1UL << SWP_TB_PTR_OFF_BITS) - 1) +#define SWP_TB_PTR_TYPE_MASK ((1UL << SWP_TB_PTR_TYPE_BITS) - 1) + +static inline bool swp_tb_is_pointer(unsigned long swp_tb) +{ + return (swp_tb & (BIT(3) - 1)) == SWP_TB_PTR_MARK; +} + +static inline unsigned long xswap_entry_to_rmap(swp_entry_t entry) +{ + unsigned long type = swp_type(entry); + unsigned long off = swp_offset(entry); + + VM_WARN_ON_ONCE(type > SWP_TB_PTR_TYPE_MASK); + VM_WARN_ON_ONCE(off > SWP_TB_PTR_OFF_MASK); + return (type << SWP_TB_PTR_TYPE_SHIFT) | + (off << SWP_TB_PTR_OFF_SHIFT) | + SWP_TB_PTR_MARK; +} + +static inline swp_entry_t xswap_rmap_to_entry(unsigned long swp_tb) +{ + unsigned long type = (swp_tb >> SWP_TB_PTR_TYPE_SHIFT) & + SWP_TB_PTR_TYPE_MASK; + unsigned long off = (swp_tb >> SWP_TB_PTR_OFF_SHIFT) & + SWP_TB_PTR_OFF_MASK; + + return swp_entry(type, off); +} +#else /* !CONFIG_XSWAP */ +static inline bool swp_tb_is_pointer(unsigned long swp_tb) +{ + return false; +} +#endif /* CONFIG_XSWAP */ + /* Macro for shadow offset calculation */ #define SWAP_COUNT_SHIFT SWP_TB_FLAGS_BITS diff --git a/mm/swapfile.c b/mm/swapfile.c index e15b20274dfb..7b6c7e9614b5 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1133,6 +1133,9 @@ static bool cluster_scan_range(struct swap_info_struct *si, swp_tb = __swap_table_get(ci, ci_off); if (swp_tb_is_null(swp_tb)) continue; + /* Backs an xswap entry: the slot is not allocatable. */ + if (swp_tb_is_pointer(swp_tb)) + return false; if (swp_tb_is_folio(swp_tb) && !__swp_tb_get_count(swp_tb)) { if (!vm_swap_full()) return false; -- 2.54.0