From nobody Wed Feb 11 08:12:13 2026 Received: from mail-ot1-f47.google.com (mail-ot1-f47.google.com [209.85.210.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0A9743624CF for ; Sun, 8 Feb 2026 21:59:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770587945; cv=none; b=euHNvAOtPDtSNyI2v5lMgb/F3LauZL80+p8ONofKVfdpnZYcfYhj5Wb130lfbGIF4wH6LLfJePbE7qvkavXK8/c+U2LuWqniVmPnAuMyr28l2pkFCMV/XFd/cG6CrPmBaHca1enDDiwaPZAJJffkUqNH8B5ub9fP5mjbHZxAbUQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770587945; c=relaxed/simple; bh=6RNDsiwA220Z2rFQxFzDnDxfVQ5PPyPpZSE3BQiIsfI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=P3oTWzzQT8eqFgUyYuMsD/w2m7+Um0l+pdPKr/qId4SCZ1MVe0HZm2p5An2pt9pvYEqspR/JiQACv5GeSfu0ZCqHvbQ1ww2nM2ksy2Vxxx0veJZ48ayBx6vCZS+Y8Ad25Lf9Xk72XuLGITCokVZjVfffVHgIynhtJhaD/UbvHw8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=g8iG3blA; arc=none smtp.client-ip=209.85.210.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="g8iG3blA" Received: by mail-ot1-f47.google.com with SMTP id 46e09a7af769-7d45d37c7a0so1532786a34.3 for ; Sun, 08 Feb 2026 13:59:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1770587944; x=1771192744; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=vwTpwkTCuWwHn2fL97emlDEhO4U5O+3cG0JKQCZZBaY=; b=g8iG3blAM2uB5QYSyUQPuoNFlNW3JKUDibmfS4bJ6jV4aF9htsWTXFfv9awoVgmjN8 nnHnxyJ67rz5tzBL1K6NFEQuJlmv0Hx5ejWysOM0CU304USpCDnyc5ystgV7VU4GJYHi hg++zwU3ptcy4JrhCb5V0oBYBeBdIEjA6iK7Gsvc6yCDCyiIBq9zO9WMv5aHylTrQxM/ 2M5+LibXTgC+53wxpmupn6wMFhqwSWZ/rD5+ZPSqYxBxUgBnC2bkxmPOlML4TmaKHbE3 LJg/rShF8W4RlF3FqjBhikqGyEoVTxZ5vSZ9aS9rH2fZOmrtfqMVMR6HZl4TinkfEtK6 ujEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770587944; x=1771192744; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=vwTpwkTCuWwHn2fL97emlDEhO4U5O+3cG0JKQCZZBaY=; b=d/8MGXNk/dAZXtLdS+bTyVCpzhYZD6E5v5I1ZwJqo0y71aVhSw1cX2zywEq0naBMZu kwmaregxbGP1Zo3ibGjGh6hofoZqiylBPX6FOCKtKo88Qdwgi7VLYvgGCkYG9U98ahd5 RViGZSI08ekvISHeufgswUFc7NZ4LTMwQqCCXbQ+lHD+s6YhgEiMRqH70YBJnXPFf4n6 HYi4J72GNEb2veYXoK/cjsVsELav0fVabG9O/iCqASgnDwRpjbbUt406dSGcNhquFeUr z/La24yUdnvALm5sTdN0JF/3LvtPKIZvfiaBn/g0QyHvsaLQ4tOPHwfCFyFwF2c7YSfK MxYw== X-Forwarded-Encrypted: i=1; AJvYcCXJkqfX1hBs6FmhXXHCiJdgr76FRD49K8uFxkQKS4X3IQMW77Ra/7Bqxv1MmgjQFeA2yudql/MQjjEW+Bo=@vger.kernel.org X-Gm-Message-State: AOJu0Yxjt9d6XSAwmpuQFI/9L22B2Bhn5kvydq+R3jdhCTXzridOFlNx 1ssYSSqtW5fACCzJDU9azzINpjlvbq6dygnyBS/iO5RPjj5B6VjhkMoD X-Gm-Gg: AZuq6aIzSpl6MmCE0blju/1fUzEImPoffoSwu8GR5UDgj4x/U0FEBQTQGVP1K2UONjH BqGQwGEij8MwgsTTWeIOzCgA9fJqEsXe0TfhwqAe0fTrCQHIp+9vfWEeqGtNWMVRaG9M6pfQxiz tjoKzkYS6xmqKwYCLqosq4s3Ns7I9WTbChptknlZRovpp5BuSaFCZhi6JAdFT9dkjlPvALo07Jf 2AxMEA4Xkc4chVXIGDDGU82QdWl2ss2tOeBdnhnEPltiiVQfJEDBmM2q5Le63SVsCW0lJvoSgCx yBfMCajS61duWZbGclfgfxH5OSpkDAfAeCk7WqBgE4LRz4I2J6PbXsBBNeb8KoonspYhWqJ+lnA 124D3/FblxR3BpxwzrLmM/PQWO+Y8bffGqCXkTZ27bliKpidzX/rwPf2XgZqFTNH/TtJxrlyDWf 2OsBVNDuUlDfObk8sGnginW/Gev5XqDEYDkw== X-Received: by 2002:a05:6830:3784:b0:7c7:266:392d with SMTP id 46e09a7af769-7d46440c217mr4795266a34.13.1770587943960; Sun, 08 Feb 2026 13:59:03 -0800 (PST) Received: from localhost ([2a03:2880:10ff:73::]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-7d4647aa6cbsm6238339a34.26.2026.02.08.13.59.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 Feb 2026 13:59:03 -0800 (PST) From: Nhat Pham To: linux-mm@kvack.org Cc: akpm@linux-foundation.org, hannes@cmpxchg.org, hughd@google.com, yosry.ahmed@linux.dev, mhocko@kernel.org, roman.gushchin@linux.dev, shakeel.butt@linux.dev, muchun.song@linux.dev, len.brown@intel.com, chengming.zhou@linux.dev, kasong@tencent.com, chrisl@kernel.org, huang.ying.caritas@gmail.com, ryan.roberts@arm.com, shikemeng@huaweicloud.com, viro@zeniv.linux.org.uk, baohua@kernel.org, bhe@redhat.com, osalvador@suse.de, lorenzo.stoakes@oracle.com, christophe.leroy@csgroup.eu, pavel@kernel.org, kernel-team@meta.com, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-pm@vger.kernel.org, peterx@redhat.com, riel@surriel.com, joshua.hahnjy@gmail.com, npache@redhat.com, gourry@gourry.net, axelrasmussen@google.com, yuanchu@google.com, weixugc@google.com, rafael@kernel.org, jannh@google.com, pfalcato@suse.de, zhengqi.arch@bytedance.com Subject: [PATCH v3 11/20] zswap: move zswap entry management to the virtual swap descriptor Date: Sun, 8 Feb 2026 13:58:24 -0800 Message-ID: <20260208215839.87595-12-nphamcs@gmail.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260208215839.87595-1-nphamcs@gmail.com> References: <20260208215839.87595-1-nphamcs@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Remove the zswap tree and manage zswap entries directly through the virtual swap descriptor. This re-partitions the zswap pool (by virtual swap cluster), which eliminates zswap tree lock contention. Signed-off-by: Nhat Pham --- include/linux/zswap.h | 6 +++ mm/vswap.c | 100 ++++++++++++++++++++++++++++++++++++++++++ mm/zswap.c | 40 ----------------- 3 files changed, 106 insertions(+), 40 deletions(-) diff --git a/include/linux/zswap.h b/include/linux/zswap.h index 1a04caf283dc8..7eb3ce7e124fc 100644 --- a/include/linux/zswap.h +++ b/include/linux/zswap.h @@ -6,6 +6,7 @@ #include =20 struct lruvec; +struct zswap_entry; =20 extern atomic_long_t zswap_stored_pages; =20 @@ -33,6 +34,11 @@ void zswap_lruvec_state_init(struct lruvec *lruvec); void zswap_folio_swapin(struct folio *folio); bool zswap_is_enabled(void); bool zswap_never_enabled(void); +void *zswap_entry_store(swp_entry_t swpentry, struct zswap_entry *entry); +void *zswap_entry_load(swp_entry_t swpentry); +void *zswap_entry_erase(swp_entry_t swpentry); +bool zswap_empty(swp_entry_t swpentry); + #else =20 struct zswap_lruvec_state {}; diff --git a/mm/vswap.c b/mm/vswap.c index d44199dc059a3..9bb733f00fd21 100644 --- a/mm/vswap.c +++ b/mm/vswap.c @@ -10,6 +10,7 @@ #include #include #include +#include #include "swap.h" #include "swap_table.h" =20 @@ -37,11 +38,13 @@ * Swap descriptor - metadata of a swapped out page. * * @slot: The handle to the physical swap slot backing this page. + * @zswap_entry: The zswap entry associated with this swap slot. * @swap_cache: The folio in swap cache. * @shadow: The shadow entry. */ struct swp_desc { swp_slot_t slot; + struct zswap_entry *zswap_entry; union { struct folio *swap_cache; void *shadow; @@ -238,6 +241,7 @@ static void __vswap_alloc_from_cluster(struct vswap_clu= ster *cluster, int start) for (i =3D 0; i < nr; i++) { desc =3D &cluster->descriptors[start + i]; desc->slot.val =3D 0; + desc->zswap_entry =3D NULL; } cluster->count +=3D nr; } @@ -1009,6 +1013,102 @@ void __swap_cache_replace_folio(struct folio *old, = struct folio *new) rcu_read_unlock(); } =20 +#ifdef CONFIG_ZSWAP +/** + * zswap_entry_store - store a zswap entry for a swap entry + * @swpentry: the swap entry + * @entry: the zswap entry to store + * + * Stores a zswap entry in the swap descriptor for the given swap entry. + * The cluster is locked during the store operation. + * + * Return: the old zswap entry if one existed, NULL otherwise + */ +void *zswap_entry_store(swp_entry_t swpentry, struct zswap_entry *entry) +{ + struct vswap_cluster *cluster =3D NULL; + struct swp_desc *desc; + void *old; + + rcu_read_lock(); + desc =3D vswap_iter(&cluster, swpentry.val); + if (!desc) { + rcu_read_unlock(); + return NULL; + } + + old =3D desc->zswap_entry; + desc->zswap_entry =3D entry; + spin_unlock(&cluster->lock); + rcu_read_unlock(); + + return old; +} + +/** + * zswap_entry_load - load a zswap entry for a swap entry + * @swpentry: the swap entry + * + * Loads the zswap entry from the swap descriptor for the given swap entry. + * + * Return: the zswap entry if one exists, NULL otherwise + */ +void *zswap_entry_load(swp_entry_t swpentry) +{ + struct vswap_cluster *cluster =3D NULL; + struct swp_desc *desc; + void *zswap_entry; + + rcu_read_lock(); + desc =3D vswap_iter(&cluster, swpentry.val); + if (!desc) { + rcu_read_unlock(); + return NULL; + } + + zswap_entry =3D desc->zswap_entry; + spin_unlock(&cluster->lock); + rcu_read_unlock(); + + return zswap_entry; +} + +/** + * zswap_entry_erase - erase a zswap entry for a swap entry + * @swpentry: the swap entry + * + * Erases the zswap entry from the swap descriptor for the given swap entr= y. + * The cluster is locked during the erase operation. + * + * Return: the zswap entry that was erased, NULL if none existed + */ +void *zswap_entry_erase(swp_entry_t swpentry) +{ + struct vswap_cluster *cluster =3D NULL; + struct swp_desc *desc; + void *old; + + rcu_read_lock(); + desc =3D vswap_iter(&cluster, swpentry.val); + if (!desc) { + rcu_read_unlock(); + return NULL; + } + + old =3D desc->zswap_entry; + desc->zswap_entry =3D NULL; + spin_unlock(&cluster->lock); + rcu_read_unlock(); + + return old; +} + +bool zswap_empty(swp_entry_t swpentry) +{ + return xa_empty(&vswap_cluster_map); +} +#endif /* CONFIG_ZSWAP */ + int vswap_init(void) { int i; diff --git a/mm/zswap.c b/mm/zswap.c index f7313261673ff..72441131f094e 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -223,37 +223,6 @@ static bool zswap_has_pool; * helpers and fwd declarations **********************************/ =20 -static DEFINE_XARRAY(zswap_tree); - -#define zswap_tree_index(entry) (entry.val) - -static inline void *zswap_entry_store(swp_entry_t swpentry, - struct zswap_entry *entry) -{ - pgoff_t offset =3D zswap_tree_index(swpentry); - - return xa_store(&zswap_tree, offset, entry, GFP_KERNEL); -} - -static inline void *zswap_entry_load(swp_entry_t swpentry) -{ - pgoff_t offset =3D zswap_tree_index(swpentry); - - return xa_load(&zswap_tree, offset); -} - -static inline void *zswap_entry_erase(swp_entry_t swpentry) -{ - pgoff_t offset =3D zswap_tree_index(swpentry); - - return xa_erase(&zswap_tree, offset); -} - -static inline bool zswap_empty(swp_entry_t swpentry) -{ - return xa_empty(&zswap_tree); -} - #define zswap_pool_debug(msg, p) \ pr_debug("%s pool %s\n", msg, (p)->tfm_name) =20 @@ -1445,13 +1414,6 @@ static bool zswap_store_page(struct page *page, goto compress_failed; =20 old =3D zswap_entry_store(page_swpentry, entry); - if (xa_is_err(old)) { - int err =3D xa_err(old); - - WARN_ONCE(err !=3D -ENOMEM, "unexpected xarray error: %d\n", err); - zswap_reject_alloc_fail++; - goto store_failed; - } =20 /* * We may have had an existing entry that became stale when @@ -1498,8 +1460,6 @@ static bool zswap_store_page(struct page *page, =20 return true; =20 -store_failed: - zs_free(pool->zs_pool, entry->handle); compress_failed: zswap_entry_cache_free(entry); return false; --=20 2.47.3