From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-179.mta1.migadu.com (out-179.mta1.migadu.com [95.215.58.179]) (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 D6ACD42089E for ; Tue, 7 Jul 2026 07:32:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.179 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783409559; cv=none; b=E4JTiPJVRYwA9kuD7QYiG4wx2iwisZG9v+h2yMhR1XjpNRFjl6cVmeg1mn8bzkviKIZGQDtiIWwIL0JDADiKi5X3xbJB6y3tOtwrM/eRODZAQxg2xMiNmL6YVaxBTSMK1YMDwss2dYz0AM3wiMY4FqpXZlAij/v4EUvY6nuBmDk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783409559; c=relaxed/simple; bh=jJBsvsLPDaYOkZlMc5EvdNCyFnn48Y88nl85cInq4Ys=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-type; b=BLSBQ9Xy76yNV+rBtJ8ldV7ZIcaOatfTLuiDo237MlCRA5QRrGjoTyKJT1BmbH3Ay+xLWHL58fih2/padaJ22x69mdhtwypwTs25GV46yRXkCtoojxz8cYtHkzssk2jVupajMjbCB+Ufi8uHpuLRxAxYjgOdtmDfvdSJFusu/UY= 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=IKnm4gSY; arc=none smtp.client-ip=95.215.58.179 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="IKnm4gSY" 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=1783409556; 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=1BvE+K5bdGTJ3dl6kLTj9wqReRw0pyGG48Az9faXHbw=; b=IKnm4gSYYpt8scKSqHBiAEHkRn6NJRwmHYqKzg3Y8Gf5h19r39u+p58gyxgm+rVMLgVxv+ m5kAwpzsOiz3z3Zegz5t9Oat3T+VjriA+v89FlhJM4FWkJ+o5RninL5tYIeMccrrmG6VuZ BNqvp91Xpra0/1MKCBhVjeNpqZQUeD4= 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 1/7] mm/swap_table: add Pointer type support for zswap entry storage Date: Tue, 7 Jul 2026 15:31:58 +0800 Message-ID: <20260707073215.72183-2-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 the Pointer type infrastructure to swap table entries, enabling direct storage of zswap_entry pointers in swap_table entries. The 0b100 marker in the low 3 bits distinguishes pointer entries from NULL (0), Shadow (0b1), and PFN (0b10) entries. Add helpers: - SWP_TB_POINTER_MARK: the 0b100 marker constant - swp_tb_is_pointer(): type check for Pointer entries - pointer_to_swp_tb(): encode a pointer into a swap table entry - swp_tb_to_pointer(): decode a swap table entry back to a pointer Signed-off-by: Baoquan He --- mm/swap_table.h | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/mm/swap_table.h b/mm/swap_table.h index e6613e62f8d0..6318bf8aa76b 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: |----------- Pointer ----------|100| - Zswap compressed entry * Bad: |------------- 1 -------------|1000| - Bad slot * * COUNT is `SWP_TB_COUNT_BITS` long, Z is the `SWP_TB_ZERO_FLAG` bit, @@ -49,9 +49,11 @@ 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: Stores a direct pointer to a zswap_entry when the swap slot's + * compressed data resides in zswap. The swap count (if any) is stored in + * the zswap_entry struct rather than inline, since the pointer occupies + * all upper bits. `0b100` is used as the marker because zswap_entry + * pointers are at least 8-byte aligned, leaving the lower three bits free. * * - Bad: Swap slot is reserved, protects swap header or holes on swap devices. */ @@ -78,6 +80,9 @@ struct swap_memcg_table { /* The first flag is zero bit (SWAP_TABLE_HAS_ZEROFLAG) */ #define SWP_TB_ZERO_FLAG BIT(BITS_PER_LONG - SWP_TB_FLAGS_BITS) +/* Pointer: zswap_entry pointer stored directly, ends with 0b100 */ +#define SWP_TB_POINTER_MARK 0b100UL + /* Bad slot: ends with 0b1000 and rests of bits are all 1 */ #define SWP_TB_BAD ((~0UL) << 3) @@ -166,6 +171,11 @@ static inline bool swp_tb_is_bad(unsigned long swp_tb) return swp_tb == SWP_TB_BAD; } +static inline bool swp_tb_is_pointer(unsigned long swp_tb) +{ + return (swp_tb & (BIT(3) - 1)) == SWP_TB_POINTER_MARK; +} + static inline bool swp_tb_is_countable(unsigned long swp_tb) { return (swp_tb_is_shadow(swp_tb) || swp_tb_is_folio(swp_tb) || @@ -188,6 +198,23 @@ static inline void *swp_tb_to_shadow(unsigned long swp_tb) return (void *)(swp_tb & ~SWP_TB_FLAGS_MASK); } +static inline unsigned long pointer_to_swp_tb(void *ptr) +{ + unsigned long val = (unsigned long)ptr; + + BUILD_BUG_ON(sizeof(unsigned long) != sizeof(void *)); + /* Pointers are 8-byte aligned, low 3 bits must be clear */ + VM_WARN_ON_ONCE(val & (BIT(3) - 1)); + return val | SWP_TB_POINTER_MARK; +} + +static inline void *swp_tb_to_pointer(unsigned long swp_tb) +{ + VM_WARN_ON(!swp_tb_is_pointer(swp_tb)); + /* Clear the low 3-bit marker to recover the original pointer */ + return (void *)(swp_tb & ~((unsigned long)(BIT(3) - 1))); +} + static inline unsigned char __swp_tb_get_count(unsigned long swp_tb) { VM_WARN_ON(!swp_tb_is_countable(swp_tb)); -- 2.54.0