From nobody Wed Feb 11 08:12:17 2026 Received: from mail-ot1-f44.google.com (mail-ot1-f44.google.com [209.85.210.44]) (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 2199134FF7F for ; Sun, 8 Feb 2026 21:58:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770587932; cv=none; b=H8O04T6VeKkLztVtpZCyvk/xXYBdzoN4hCZjc/eWteVUdqVQ/Fct4sLlknVfFjmbSvgTtBfAlQCBQw+bYdolhEem+luqyDdXzuXQ7SOUbuuTLJGGo2ehWv+JgX8lMkFhUWleU5jQFl7yqxlodjH+JCMquhVkJBq020EXJq6QJeM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770587932; c=relaxed/simple; bh=ODJpTK6yx8sL3Yjx3SR46wf2FGw+h0vJmlVT0y2AyfQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=m5DoWipOp/bDGa0Yq0G4YGdHqXwBCQ54NHtJ30GsdZptD3tQUQcHzrATtABvL7gDD5QPTTvBqlaKYt4tqgShbW8q4xRHENq2VNWV1KAVT9EIApcZO/BeYhoyeR5M/UFZgpTPityMn9oQN4Xhg88noJ4xVSsi4Gefm5AL661GM4A= 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=dG9qI0kY; arc=none smtp.client-ip=209.85.210.44 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="dG9qI0kY" Received: by mail-ot1-f44.google.com with SMTP id 46e09a7af769-7d196a2334fso3286672a34.1 for ; Sun, 08 Feb 2026 13:58:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1770587931; x=1771192731; 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=HpwswEDlCandEKSEcjjhsX6vu11jfmhgbFRVqdS0y6A=; b=dG9qI0kYQmhW6JNCk95pxW7PPQil8zbjJFz87Ku7Nkh86EZVGIPPfNKoiPpKUmt7ah Ca/uVBL1FGUlsicpkOLBKVBeF8Tm6yKbgdvfnUokxsG2Jwl0ef/NyQXx2j9P4TrIDzYQ +hAZaq5nvpUYgyR3rDhRKvq/tpfPLSaLXoa3qfqak/nQGQwtpqJUL34fGjOXlW2V5v8E OfF4NhluWoIili8uqIAamc0ElWDSY4pF9uaYeCuv36FG7XyWD1YlHXboNTxwbZ/CfZ0q R8EKETELmcXLN8hUii6EH7dj+Z/os38EJ6fiUWe+uTIzESMFDTcEh+te/nqiVJyE429D 7Ajg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770587931; x=1771192731; 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=HpwswEDlCandEKSEcjjhsX6vu11jfmhgbFRVqdS0y6A=; b=DAbVfdnjcUP3cg2DlvJXl3lWjI5PbUi0k+z3aScdofOpPRK8oSu7LqTmZGFs1w+fFn PpMMwybFyWxqNYaiCHpdfoD8RBXvPGcpaHHp9+dJ79v4REswX//1wC4s1yHrJcsAZeyM q2awsiO/kp2bYkSv2jCN7AGylfOnqdKoT/+c3IPxrvJ366Yp5mYcV9F47Is23P/trIEz 96j5FVCLg71z+Wr+tPsg2hADM3CRksREdzsX4APncYKzuVIUvr7d2pCdI7zQI5ZC5zhh sEAYSn9b2Fqn7ld2CoDpFESTaWPr4OeTsOD6ZmuN6IDbWFQWHdehGWJOcgRycS3PLwn0 k4CA== X-Forwarded-Encrypted: i=1; AJvYcCVNsgt4Fe08HCSG1UtGbuLxWCfGkKpxfcCO6crBy7ub/AyCOVpvoIg1CEHI9b5tAjg+bTECmeYb0+GTa5k=@vger.kernel.org X-Gm-Message-State: AOJu0YwNeDjWhJRFqRX6FmPx/V8JP9UyMLxeNE0cfbzK5UbFNe1BSPFj nBidT3WDFmbqhZ3fHKzaW+pYFu1XoS25JcHP41T8psHei1bRlpdzGpJw X-Gm-Gg: AZuq6aIdB0X9QG5nfHHRsmfI1Cbm6XqZUT08HL7fpkPGCjStDIyEiGkmz/4Jx3vcu3/ saZrc9o8cKgmsGInvlUzfWpKB023BvigunJ10wfa494j2RffAdbU0puRznvTFWLB1Dq23/luElJ BninikHwIH5MgpKngVvT8w5yc3kKgFIhvX9XKDk9XQtmEIkSjgHTOREb7EzB2vtgR6eaGJutBZ1 JwxIrCDBYGLb44mvlFpPABzLHuEE5G2O56BCPxliFlAFz302RsKxTnckpu0IaapxyGQuo6eFaKf jH956vkMsCodqeHzlsa1bEQKtfMyJWNcQ6R0Ks8H2R/Azm17dWsks58OoHMttnCpLgB3rGbYhqV QrJ6X/UkT62dkZE3b1v+7QN+ZEgOiVmlUZ1TWKKVe3RGbTxul+dcmpwoS3EVYTmVKt2tAF8qTs5 sLdUqPTOBEE+oZsuuZht88MNdMUDHzvI2h/g== X-Received: by 2002:a05:6830:828c:b0:7d1:93ea:5bc0 with SMTP id 46e09a7af769-7d4633df66dmr5296308a34.9.1770587931097; Sun, 08 Feb 2026 13:58:51 -0800 (PST) Received: from localhost ([2a03:2880:10ff:70::]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-7d4646fb89fsm6346653a34.1.2026.02.08.13.58.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 Feb 2026 13:58:50 -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 04/20] zswap: add new helpers for zswap entry operations Date: Sun, 8 Feb 2026 13:58:17 -0800 Message-ID: <20260208215839.87595-5-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" Add new helper functions to abstract away zswap entry operations, in order to facilitate re-implementing these functions when swap is virtualized. Signed-off-by: Nhat Pham --- mm/zswap.c | 59 ++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 40 insertions(+), 19 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index 315e4d0d08311..a5a3f068bd1a6 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -234,6 +234,38 @@ static inline struct xarray *swap_zswap_tree(swp_entry= _t swp) >> ZSWAP_ADDRESS_SPACE_SHIFT]; } =20 +static inline void *zswap_entry_store(swp_entry_t swpentry, + struct zswap_entry *entry) +{ + struct xarray *tree =3D swap_zswap_tree(swpentry); + pgoff_t offset =3D swp_offset(swpentry); + + return xa_store(tree, offset, entry, GFP_KERNEL); +} + +static inline void *zswap_entry_load(swp_entry_t swpentry) +{ + struct xarray *tree =3D swap_zswap_tree(swpentry); + pgoff_t offset =3D swp_offset(swpentry); + + return xa_load(tree, offset); +} + +static inline void *zswap_entry_erase(swp_entry_t swpentry) +{ + struct xarray *tree =3D swap_zswap_tree(swpentry); + pgoff_t offset =3D swp_offset(swpentry); + + return xa_erase(tree, offset); +} + +static inline bool zswap_empty(swp_entry_t swpentry) +{ + struct xarray *tree =3D swap_zswap_tree(swpentry); + + return xa_empty(tree); +} + #define zswap_pool_debug(msg, p) \ pr_debug("%s pool %s\n", msg, (p)->tfm_name) =20 @@ -1000,8 +1032,6 @@ static bool zswap_decompress(struct zswap_entry *entr= y, struct folio *folio) static int zswap_writeback_entry(struct zswap_entry *entry, swp_entry_t swpentry) { - struct xarray *tree; - pgoff_t offset =3D swp_offset(swpentry); struct folio *folio; struct mempolicy *mpol; bool folio_was_allocated; @@ -1040,8 +1070,7 @@ static int zswap_writeback_entry(struct zswap_entry *= entry, * old compressed data. Only when this is successful can the entry * be dereferenced. */ - tree =3D swap_zswap_tree(swpentry); - if (entry !=3D xa_load(tree, offset)) { + if (entry !=3D zswap_entry_load(swpentry)) { ret =3D -ENOMEM; goto out; } @@ -1051,7 +1080,7 @@ static int zswap_writeback_entry(struct zswap_entry *= entry, goto out; } =20 - xa_erase(tree, offset); + zswap_entry_erase(swpentry); =20 count_vm_event(ZSWPWB); if (entry->objcg) @@ -1427,9 +1456,7 @@ static bool zswap_store_page(struct page *page, if (!zswap_compress(page, entry, pool)) goto compress_failed; =20 - old =3D xa_store(swap_zswap_tree(page_swpentry), - swp_offset(page_swpentry), - entry, GFP_KERNEL); + old =3D zswap_entry_store(page_swpentry, entry); if (xa_is_err(old)) { int err =3D xa_err(old); =20 @@ -1563,11 +1590,9 @@ bool zswap_store(struct folio *folio) unsigned type =3D swp_type(swp); pgoff_t offset =3D swp_offset(swp); struct zswap_entry *entry; - struct xarray *tree; =20 for (index =3D 0; index < nr_pages; ++index) { - tree =3D swap_zswap_tree(swp_entry(type, offset + index)); - entry =3D xa_erase(tree, offset + index); + entry =3D zswap_entry_erase(swp_entry(type, offset + index)); if (entry) zswap_entry_free(entry); } @@ -1599,9 +1624,7 @@ bool zswap_store(struct folio *folio) int zswap_load(struct folio *folio) { swp_entry_t swp =3D folio->swap; - pgoff_t offset =3D swp_offset(swp); bool swapcache =3D folio_test_swapcache(folio); - struct xarray *tree =3D swap_zswap_tree(swp); struct zswap_entry *entry; =20 VM_WARN_ON_ONCE(!folio_test_locked(folio)); @@ -1619,7 +1642,7 @@ int zswap_load(struct folio *folio) return -EINVAL; } =20 - entry =3D xa_load(tree, offset); + entry =3D zswap_entry_load(swp); if (!entry) return -ENOENT; =20 @@ -1648,7 +1671,7 @@ int zswap_load(struct folio *folio) */ if (swapcache) { folio_mark_dirty(folio); - xa_erase(tree, offset); + zswap_entry_erase(swp); zswap_entry_free(entry); } =20 @@ -1658,14 +1681,12 @@ int zswap_load(struct folio *folio) =20 void zswap_invalidate(swp_entry_t swp) { - pgoff_t offset =3D swp_offset(swp); - struct xarray *tree =3D swap_zswap_tree(swp); struct zswap_entry *entry; =20 - if (xa_empty(tree)) + if (zswap_empty(swp)) return; =20 - entry =3D xa_erase(tree, offset); + entry =3D zswap_entry_erase(swp); if (entry) zswap_entry_free(entry); } --=20 2.47.3