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 4F609606A32D for ; Thu, 3 Dec 2026 22:01:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=Y3nWPjJP Received: by kanga.kvack.org (Postfix) id 4DE556B05C4; Thu, 3 Dec 2026 17:00:54 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4B4E56B05C5; Thu, 3 Dec 2026 17:00:54 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 355786B05C6; Thu, 3 Dec 2026 17:00:54 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id DE10E6B05C4 for ; Thu, 3 Dec 2026 17:00:53 -0500 (EST) Received: from smtpin20.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay05.hostedemail.com (Postfix) with ESMTP id CFEAA401A2 for ; Thu, 3 Dec 2026 22:00:52 +0000 (UTC) X-FDA: 85504042344.20.4B75879 Received: from mail-yx1-f44.google.com (mail-yx1-f44.google.com [74.125.224.44]) by imf26.hostedemail.com (Postfix) with ESMTP id 9D6B3140014 for ; Thu, 3 Dec 2026 22:00:48 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=gmail.com header.s=20260707 header.b=Y3nWPjJP; spf=pass (imf26.hostedemail.com: domain of nphamcs@gmail.com designates 74.125.224.44 as permitted sender) smtp.mailfrom=nphamcs@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796335248; b=C8mAzz7fTXfbnuDE1FJ9lJ9m8VsinxsSfy05STa6H0Vhie9nXQemPUtPW7xISeoWuv1PY4 ZZT0T7KT7du3cKPSnI+KzWhhIJPXp7sNgh5ghNf1qSQ6mxHZKqWEVVwYAilOtjzNtEc+Cp Ym8Xd+RL7YvXtk5JDRMR7TK1qgebn/A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796335248; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=v9WQBM+L4C1aIdBGwc0+b9IXu0r5QbwGvfM9Jbxz/ek=; b=MRQBWQKxXAvpDwD8lk5Ssskaz1hDH/lIahHB7hkoPN/89n3sKpCb17g1o6Tw3jhQtbV/9V W5hiZfQ5mhFw2R2bbfgA3qdxSN4GloM/JoPGe6WNkZs+H3H6ABCjAO8K0PTH/VvzSe/Wdt kCyyyBTjGGIxTGe6EIYdfT24GXbChXM= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=gmail.com header.s=20260707 header.b=Y3nWPjJP; spf=pass (imf26.hostedemail.com: domain of nphamcs@gmail.com designates 74.125.224.44 as permitted sender) smtp.mailfrom=nphamcs@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-yx1-f44.google.com with SMTP id 956f58d0204a3-67da564d9f2so317400d50.1 for ; Thu, 03 Dec 2026 14:00:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796335247; x=1796940047; darn=kvack.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:content-type; bh=v9WQBM+L4C1aIdBGwc0+b9IXu0r5QbwGvfM9Jbxz/ek=; b=Y3nWPjJP8fhjuZ6sbAdfNP56hlYXdtNyutBAkbdRj8ORyu0wS83tcXCvLfuaWeDtI7 uBHWaGtSaYN0cPpKU33JsjEanYS44V3LzfBkIZMmsUpzFD5QjiEOHn2cfXQ++5K10iHw TXJChFfZKlCeyXrdM6nS71Ip2zwhYAW8gIaowb06eOGTWDp63ZT8pYIzydgF09zW6LM0 yUQxo2kJW0baIiu6mOt+6na6KRIQ7+6NpuidCy6D1wAtN0sAvVCx+8YGtzornfcqd4J2 smUc05oLU68K2CQ7mgmV+w910apphnvhVCo1lxoMlg5ymdaVOm6KX5Z0LpekzkNNkgXp 5xAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796335247; x=1796940047; 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:content-type; bh=v9WQBM+L4C1aIdBGwc0+b9IXu0r5QbwGvfM9Jbxz/ek=; b=NekKtMl8NFDDBvE7u6WBl49FdDvE/CQTyDgzDbjQ60c7RmU5Ok6CNiE2zoI2qrewaN S+674Ht8r1orZui1bXnL6+bFA9iJA3dBGePzxOEvyAo4QZbAVja/7TnCQ76RIvB/u4tm RggTN1IcSJs6UGQbH6KcHWSBJvuCjSiWjfa2PNSlof8I7FdDXT3YZMe6QbvjNNLXO8B8 Zt3DDxxMVHkW3V3w0YoGwTf7zdWBc9p2j7Bm8fWWpC9Ixjo+aQbeDORd4JgYSbBFtASC H6opgG4YpzDwmJk5T0xzMwgaD1XHCKgOvvPORi+HOPpaw9MstaVBKFMeQlnOAL9Nk2Ys 7JxQ== X-Gm-Message-State: APuU4Z/vFvKTRNfKvabmjuBt9TIBPo1LeYUiKCDk7KnK6G+YGRTzzcbU lgLoAaAwUizm5RfYAMUwCCg0WCU+g42v1TFuRAFq1lXT8gZkBN7Z+N3ZGDvku/cj X-Gm-Gg: AZFdTZc6LHGS1kn5H7GRr8rAqSTT7pEzsqJO2qOwKVXdD4sALTE8aaoCVPRZGHU1xoD ujfmdb6XeyMt4iBe/5WpikMUTDjcOU1MgdAumK4LcX4w/fv+JtEuP/ctriaxbvpsin/qPZfxtsR 6+BcS2xPD9LpjeQ1CGigLevPv0j1c6Sm6VIqKuwdOK8UiUlMtfy09qTAzsw5TOtOyDwLZ7hrgQI s0ANmaB31Fg85Ybif6mJtzQZpxLyYOYXXFtwYdhwucXqfEWEjbIDoRDYnyBXE9VnE5HwtMK1By4 rx5WmktYipJmIAsZHupLTDFzJW/Sl2S2gADwoBG6nCmQhNg/YsUoPkhbw0GlsPD4OWEUv7mpYvv Ro/FJay3S5KNnL6YLc2pKsnANbm+7wNelzLk1qE5ykAvQP5boGwe5kQRFbbT33LC/ap6y5TyTf9 mcnVac9uxFaB3b7+luFEkYeB3pgMxjjnDslZyDX7kRRr0BQZH16v1QcuNOxf2qtcbkfkz1kyifo 53gKFLZKBVH4Gje91X4JcQabF82ylqZwGJeSsek2m0JHvX7LscKwVtwUkQBbNan8lbsXhTCnMq2 RDikGjF/Ebuc/lT78YR4ZJSxDjRYVv3LWEh+Rwca+BG2dbuUuJPVTqGlSFcfKSlIXT4JC1+LWn2 wIADgGqAkMxEfw8eJJpgJOJ1mBNsh15bMLFCgOA== X-Received: by 2002:a05:690e:4089:b0:67e:ad77:e538 with SMTP id 956f58d0204a3-67f72423437mr63575d50.67.1796335246202; Thu, 03 Dec 2026 14:00:46 -0800 (PST) Received: from localhost ([2a03:2880:10ff:5f::]) by smtp.gmail.com with ESMTPSA id 956f58d0204a3-67f0cd04b79sm206551d50.10.2026.12.03.14.00.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 14:00:45 -0800 (PST) From: Nhat Pham To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Andrew Morton , Chris Li , Kairui Song , Kemeng Shi , Baoquan He , Barry Song , Youngjun Park , Johannes Weiner , Yosry Ahmed , Chengming Zhou , Qi Zheng , Shakeel Butt , Axel Rasmussen , Yuanchu Xie , Wei Xu , Baolin Wang , Michal Hocko , David Hildenbrand , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Hugh Dickins , Jonathan Corbet , Shuah Khan , Randy Dunlap , Roman Gushchin , Muchun Song , Arnd Bergmann , Karl Mehltretter , Dapeng Mi , Marco Elver , Ethan Nelson-Moore , "Borislav Petkov (AMD)" , Jeff Layton , linux-doc@vger.kernel.org, cgroups@vger.kernel.org, Tejun Heo , =?UTF-8?q?Michal=20Koutn=C3=BD?= Subject: [PATCH v7 18/20] mm/zswap: store compressed entries only in the vtable Date: Thu, 3 Dec 2026 14:00:08 -0800 Message-ID: <20261203220010.1302207-19-nphamcs@gmail.com> X-Mailer: git-send-email 2.53.1 In-Reply-To: <20261203220010.1302207-1-nphamcs@gmail.com> References: <20261203220010.1302207-1-nphamcs@gmail.com> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Stat-Signature: z1681aef34j9j4zuwtta1ii16nk846mi X-Rspam-User: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, 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-Queue-Id: 9D6B3140014 X-Rspamd-Server: rspam01 X-HE-Tag: 1796335248-6265 X-HE-Meta: U2FsdGVkX18UifeBrAZEBkglu3q79GReUf+Szutb9R8kMdiCxlVWBNvAhZDdGsJ/zmEis4wsN95JUP+4KfucuWeHU4oGPExFIr+ugYN/fQJCjqk9NBRCdk15HJlBp5HQl5fNVM6MwRw9nvLRD1iYZyQA8mU/bfy4TUeZssCCOqYOYtVfeiNeEVgCIMGZ5y/VkV+MekUC2wUxGUKIsOdradbn9xLFgbkMeYO2Ucf5UrBlEN9gXsIGRsDJXkPT6/hhXgIeAFPu43p1PKvNo/QDnHlynGSJQbHT3r1po7PdJCKkFruQSEbnf+ixWOas2XGVK0MU68uDQYzH0bqWzDWZ0o/GSxZSMpF4eGcmQgUCxxeO5Hv2yqSsl1+uqFmTS9ZCOPBQId/XZDyXn2EYSh0qj4eBvhujtj+vpfpHbH2kIrinj2rebhRcBnfhiaV6SDPsmJlQunOQ7Jd1sT1CfPPuudh9Uo17WzE1QfcMGxNrD5MrNvrWafY3mBwKUFJrIvqtDH9l4ca1ZzoIHc2VqlRGUPhUd/cxj9H92aHC/0ucsmjgPnUmAkMDUS9iS54WG/CMDxSKB6FVWaSGYlIH7RxjW5IftZGiyMNpy1l0+JWpkotH4YkGNdpW+gIGXu1rfVM9SXLlgv/5kVxmW+kXXS2NqxvUD6deMvm0O1jQKiCXWKVDnujUr+M9pOya9+c9btN1cdxCC/8/bAuAYdr1QMd749XWej5JCZnHFIaxnoTuwwoJsaIANlwvU33pW++PxVEZ1U44hDIQiFMJEZ0DvyYiR36ykR7LcKRx4o5pr9GL3O9GoV4EgrNwVJd49uJzuwnPmqSo5Frs3ju017S9Eys6gYPzzPBWtnTXb4WGernQdtlwMAr7cE9tj5KRa2NrzFpuoUbFxwrBtN6+KLHzJUp87yZp6vijq2+T6JkmC0N+xg+jCYcGWHsBewLMoHMhu9uorSNLWRRGYAAcjTUF23/ RNrFK8C8 6vja6t4TXe0GB9f0qAWWBO9j9Jh6qL9RFgsjD40B73TjJW/fQ0p3Z9aYnZMDfObR84dq5ebRSN3YhxOQVAp6rcrQ+Vr8elcd17EIL9ZZxdd3/5NmNpXNZE6FVJYmEI2rEOn7sQnV4lkhu3cE8BNbKxhnFog0VwLZdtDZfRAFFRuLnMFGm8peIKE5WkU77MK6QqoDw3vy/AEBCbcwz+T31ttFBYSISc/z9P7K2ZAxmXzhrhj0hoGTC9Zp/MazIFbNkqNEKTznA56L3BAv9l6KA7hmj4B2/s9eBnFN4v3WmIg1NLTrHXPRaBt2Xxblhn4MzYf6Om71hvzHr6//hJq0LwGBQZUGalQP8UusEtItf2ezblMYzIZRfFwkvInGtk9aKwKkNJw1vOgyPsmZyHJwpBoyN2uPGTCkGvmDUVslAyG+Orp5+lbjfAg3uBt3iBU/bMHDEUNGxKZPcaRFIKXe2edr5/klHayZHXbWut90sPP9OtTE3Jhk7nikiiwPVdJLElEBr/mUVQmGmNG9sJf8gN5SDvjfOkAkMWHARuDH1i7NnpGhKBeh1cO8J8R7t6ZK9xwzB88F8m+F/fFHZoVuMlt9PN56slFnBtff5N44nyqIfkXG7IvXBRt/H7XzZWgNtLFa3vHzU3Jxe+uMOLTAu1Ny3hv2/1Is5OzsKPLm8dgtIeDT1ayvRuqnfmcao6qELTeMSxo/Cu+2AzGioHoMVpbQEE9le1l4Cyd09blGOl4WChT0= 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 Vswap is mandatory and supports 32-bit systems, so the vtable supplies every zswap lookup for decompression and writeback. Remove the redundant per-device XArrays and call the virtual entry helpers directly. Signed-off-by: Nhat Pham --- Documentation/admin-guide/mm/zswap.rst | 12 +- include/linux/zswap.h | 13 -- mm/swapfile.c | 12 +- mm/zswap.c | 185 +++---------------------- 4 files changed, 28 insertions(+), 194 deletions(-) diff --git a/Documentation/admin-guide/mm/zswap.rst b/Documentation/admin-guide/mm/zswap.rst index 2464425c783d..6dbe6ed7112a 100644 --- a/Documentation/admin-guide/mm/zswap.rst +++ b/Documentation/admin-guide/mm/zswap.rst @@ -59,19 +59,15 @@ returned by the allocation routine and that handle must be mapped before being accessed. The compressed memory pool grows on demand and shrinks as compressed pages are freed. The pool is not preallocated. -When a swap page is passed from swapout to zswap, zswap maintains a mapping of -the swap entry, a combination of the swap type and swap offset, to the zsmalloc -handle that references that compressed swap page. This mapping is achieved -with an xarray per swap type. The swap offset is the search key for the xarray -nodes. +When zswap compresses a swap page, it stores the zsmalloc handle in a zswap +entry. The corresponding virtual swap slot points to that entry in its vtable. During a page fault on a PTE that is a swap entry, the swapin code calls the zswap load function to decompress the page into the page allocated by the page fault handler. -Once there are no PTEs referencing a swap page stored in zswap (i.e. the count -in the swap_map goes to 0) the swap code calls the zswap invalidate function -to free the compressed entry. +When the swap code releases the virtual swap slot, it frees any remaining +compressed entry. Zswap seeks to be simple in its policies. Sysfs attributes allow for one user controlled policy: diff --git a/include/linux/zswap.h b/include/linux/zswap.h index b90f330f87cd..08bbb567b83e 100644 --- a/include/linux/zswap.h +++ b/include/linux/zswap.h @@ -28,10 +28,7 @@ struct zswap_lruvec_state { unsigned long zswap_total_pages(void); bool zswap_store(struct folio *folio); int zswap_load(struct folio *folio); -void zswap_invalidate(int type, pgoff_t offset, unsigned long nr_entries); void zswap_entry_free(struct zswap_entry *entry); -int zswap_swapon(int type, unsigned long nr_pages); -void zswap_swapoff(int type); void zswap_memcg_offline_cleanup(struct mem_cgroup *memcg); void zswap_lruvec_state_init(struct lruvec *lruvec); void zswap_folio_swapin(struct folio *folio); @@ -52,18 +49,8 @@ static inline int zswap_load(struct folio *folio) return -ENOENT; } -static inline void zswap_invalidate(int type, pgoff_t offset, - unsigned long nr_entries) -{ -} - static inline void zswap_entry_free(struct zswap_entry *entry) {} -static inline int zswap_swapon(int type, unsigned long nr_pages) -{ - return 0; -} -static inline void zswap_swapoff(int type) {} static inline void zswap_memcg_offline_cleanup(struct mem_cgroup *memcg) {} static inline void zswap_lruvec_state_init(struct lruvec *lruvec) {} static inline void zswap_folio_swapin(struct folio *folio) {} diff --git a/mm/swapfile.c b/mm/swapfile.c index 8f38737d8aa6..d71fccce5ae3 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1603,9 +1603,6 @@ static void swap_range_free(struct swap_info_struct *si, unsigned long offset, unsigned long end = offset + nr_entries - 1; void (*swap_slot_free_notify)(struct block_device *, unsigned long); - if (!swap_is_vswap(si)) - zswap_invalidate(si->type, offset, nr_entries); - if (si->flags & SWP_BLKDEV) swap_slot_free_notify = si->bdev->bd_disk->fops->swap_slot_free_notify; @@ -4086,7 +4083,6 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) spin_unlock(&p->lock); spin_unlock(&swap_lock); arch_swap_invalidate_area(p->type); - zswap_swapoff(p->type); mutex_unlock(&swapon_mutex); kfree(p->global_cluster); p->global_cluster = NULL; @@ -4715,10 +4711,6 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) } } - error = zswap_swapon(si->type, maxpages); - if (error) - goto bad_swap_unlock_inode; - /* * Flush any pending IO and dirty mappings before we start using this * swap device. @@ -4727,7 +4719,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) error = inode_drain_writes(inode); if (error) { inode->i_flags &= ~S_SWAPFILE; - goto free_swap_zswap; + goto bad_swap_unlock_inode; } mutex_lock(&swapon_mutex); @@ -4761,8 +4753,6 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) error = 0; goto out; -free_swap_zswap: - zswap_swapoff(si->type); bad_swap_unlock_inode: inode_unlock(inode); bad_swap: diff --git a/mm/zswap.c b/mm/zswap.c index e527a34e52a8..c4fb6af8b300 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -158,12 +158,6 @@ struct crypto_acomp_ctx { struct zswap_acomp_req decomp; /* unused with on-stack requests */ }; -/* - * The lock ordering is zswap_tree.lock -> zswap_pool.lru_lock. - * The only case where lru_lock is not acquired while holding tree.lock is - * when a zswap_entry is taken off the lru for writeback, in that case it - * needs to be verified that it's still valid in the tree. - */ struct zswap_pool { struct zs_pool *zs_pool; struct crypto_acomp_ctx __percpu *acomp_ctx; @@ -185,7 +179,11 @@ struct zswap_pool { static DEFINE_XARRAY_FLAGS(zswap_pools, XA_FLAGS_ALLOC1 | XA_FLAGS_LOCK_BH); static struct zswap_pool __rcu *zswap_current_pool; -/* Global LRU lists shared by all zswap pools. */ +/* + * Global LRU lists shared by all zswap pools. Releasing an entry from the + * vtable takes the cluster lock before the LRU lock. Writeback drops the + * LRU lock before revalidating the entry under the cluster lock. + */ static struct list_lru zswap_list_lru; /* The lock protects zswap_next_shrink updates. */ @@ -200,7 +198,7 @@ static struct shrinker *zswap_shrinker; * This structure contains the metadata for tracking a single compressed * page within zswap. * - * swpentry - associated swap entry, the offset indexes into the xarray + * swpentry - virtual swap entry whose vtable slot holds this zswap entry * length - the length in bytes of the compressed page data. Needed during * decompression. * referenced - true if the entry recently entered the zswap pool. Unset by the @@ -232,9 +230,6 @@ static struct zswap_pool *zswap_entry_pool(struct zswap_entry *entry) return xa_load(&zswap_pools, entry->pool_idx); } -static struct xarray *zswap_trees[MAX_SWAPFILES]; -static unsigned int nr_zswap_trees[MAX_SWAPFILES]; - /* pool counter to provide unique names to zsmalloc */ static atomic_t zswap_pools_count = ATOMIC_INIT(0); @@ -256,39 +251,6 @@ static bool zswap_has_pool; * helpers and fwd declarations **********************************/ -/* One swap address space for each 64M swap space */ -#define ZSWAP_ADDRESS_SPACE_SHIFT 14 -#define ZSWAP_ADDRESS_SPACE_PAGES (1 << ZSWAP_ADDRESS_SPACE_SHIFT) - -static inline struct xarray *zswap_tree(int type, pgoff_t offset) -{ - return &zswap_trees[type][offset >> ZSWAP_ADDRESS_SPACE_SHIFT]; -} - -static inline struct xarray *swap_zswap_tree(swp_entry_t swp) -{ - return zswap_tree(swp_type(swp), swp_offset(swp)); -} - -static struct zswap_entry *zswap_entry_load(swp_entry_t swp) -{ - if (is_vswap_entry(swp)) - return vswap_zswap_load(swp); - return xa_load(swap_zswap_tree(swp), swp_offset(swp)); -} - -static struct zswap_entry *zswap_entry_store(swp_entry_t swp, - struct zswap_entry *entry) -{ - if (is_vswap_entry(swp)) { - vswap_zswap_store(swp, entry); - return NULL; - } - - return xa_store(swap_zswap_tree(swp), swp_offset(swp), entry, - GFP_KERNEL); -} - #define zswap_pool_debug(msg, p) \ pr_debug("%s pool %s\n", msg, (p)->tfm_name) @@ -1138,7 +1100,7 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx, * old compressed data. Only when this is successful can the entry * be dereferenced. */ - if (entry != zswap_entry_load(swpentry)) { + if (entry != vswap_zswap_load(swpentry)) { ret = -ENOMEM; goto err; } @@ -1264,8 +1226,7 @@ static enum lru_status shrink_memcg_cb(struct list_head *item, struct list_lru_o * * 1. We extract the swp_entry_t to the stack, allowing * zswap_writeback_entry() to pin the swap entry and - * then validate the zswap entry against that swap entry's - * tree using pointer value comparison. Only when that + * then validate the pointer against the vtable slot. Only when that * is successful can the entry be dereferenced. * * 2. Usually, objects are taken off the LRU for reclaim. In @@ -1288,7 +1249,7 @@ static enum lru_status shrink_memcg_cb(struct list_head *item, struct list_lru_o /* * Once the lru lock is dropped, the entry might get freed. The * swpentry is copied to the stack, and entry isn't deref'd again - * until the entry is verified to still be alive in the tree. + * until writeback verifies it against the vtable. */ swpentry = entry->swpentry; @@ -1588,7 +1549,7 @@ static bool zswap_store_page(struct folio *folio, long index, struct zswap_pool *pool) { swp_entry_t page_swpentry = folio_swap_entry(folio, index); - struct zswap_entry *entry, *old; + struct zswap_entry *entry; /* allocate entry */ entry = zswap_entry_cache_alloc(GFP_KERNEL, folio_nid(folio)); @@ -1607,29 +1568,14 @@ static bool zswap_store_page(struct folio *folio, long index, */ entry->pool_idx = pool->idx; - old = zswap_entry_store(page_swpentry, entry); - if (xa_is_err(old)) { - int err = xa_err(old); - - WARN_ONCE(err != -ENOMEM, "unexpected xarray error: %d\n", err); - zswap_reject_alloc_fail++; - goto store_failed; - } + vswap_zswap_store(page_swpentry, entry); /* - * We may have had an existing entry that became stale when - * the folio was redirtied and now the new version is being - * swapped out. Get rid of the old. - */ - if (old) - zswap_entry_free(old); - - /* - * The entry is successfully compressed and stored in the tree, there is + * The entry is successfully compressed and stored, so there is * no further possibility of failure. Grab refs to the pool and objcg, * charge zswap memory, and increment zswap_stored_pages. * The opposite actions will be performed by zswap_entry_free() - * when the entry is removed from the tree. + * when the entry is removed from the vtable. */ zswap_pool_get(pool); if (objcg) { @@ -1641,7 +1587,7 @@ static bool zswap_store_page(struct folio *folio, long index, atomic_long_inc(&zswap_stored_incompressible_pages); /* - * We finish initializing the entry while it's already in xarray. + * We finish initializing the entry while it's already in the vtable. * This is safe because: * * 1. Concurrent stores and invalidations are excluded by folio lock. @@ -1658,8 +1604,6 @@ static bool zswap_store_page(struct folio *folio, long index, return true; -store_failed: - zs_free(pool->zs_pool, &entry->handle); compress_failed: zswap_entry_cache_free(entry); return false; @@ -1668,7 +1612,6 @@ static bool zswap_store_page(struct folio *folio, long index, bool zswap_store(struct folio *folio) { long nr_pages = folio_nr_pages(folio); - swp_entry_t swp = folio->swap; struct obj_cgroup *objcg = NULL; struct mem_cgroup *memcg = NULL; struct zswap_pool *pool; @@ -1730,17 +1673,10 @@ bool zswap_store(struct folio *folio) return ret; /* - * If the zswap store fails or zswap is disabled, we must invalidate - * the possibly stale entries which were previously stored at the - * offsets corresponding to each page of the folio. Otherwise, - * writeback could overwrite the new data in the swapfile. + * Drop any entries installed before a partial store failed. */ - if (is_vswap_entry(swp)) { - if (index > 0) - folio_release_non_phys_swap_backing(folio); - } else { - zswap_invalidate(swp_type(swp), swp_offset(swp), nr_pages); - } + if (index > 0) + folio_release_non_phys_swap_backing(folio); return ret; } @@ -1756,32 +1692,15 @@ bool zswap_store(struct folio *folio) */ bool zswap_is_present(swp_entry_t entry, unsigned int nr) { - pgoff_t offset = swp_offset(entry); - struct xarray *tree; - unsigned long index = offset; unsigned int i; - /* Virtual entries have no native zswap tree. */ - if (is_vswap_entry(entry)) { - for (i = 0; i < nr; i++) - if (vswap_zswap_load(swp_entry(swp_type(entry), offset + i))) - return true; - return false; + for (i = 0; i < nr; i++) { + if (vswap_zswap_load(entry)) + return true; + entry.val++; } - tree = swap_zswap_tree(entry); - - if (zswap_never_enabled() || xa_empty(tree)) - return false; - /* - * A pinned range is at most SWAPFILE_CLUSTER slots and is aligned to - * its own size, so one tree covers all of it and a single lookup is - * enough. Scanning only part of the range would report a false - * "absent" and let the caller read a stale copy from the device. - */ - BUILD_BUG_ON(SWAPFILE_CLUSTER > ZSWAP_ADDRESS_SPACE_PAGES); - - return xa_find(tree, &index, offset + nr - 1, XA_PRESENT); + return false; } /** @@ -1817,7 +1736,7 @@ int zswap_load(struct folio *folio) if (folio_test_large(folio)) return -ENOENT; - entry = zswap_entry_load(swp); + entry = vswap_zswap_load(swp); if (!entry) return -ENOENT; @@ -1847,64 +1766,6 @@ int zswap_load(struct folio *folio) return 0; } -void zswap_invalidate(int type, pgoff_t offset, unsigned long nr_entries) -{ - struct zswap_entry *entry; - struct xarray *tree; - unsigned long i; - - if (zswap_never_enabled()) - return; - - for (i = 0; i < nr_entries; i++) { - tree = zswap_tree(type, offset + i); - - if (xa_empty(tree)) - continue; - - entry = xa_erase(tree, offset + i); - if (entry) - zswap_entry_free(entry); - } -} - -int zswap_swapon(int type, unsigned long nr_pages) -{ - struct xarray *trees, *tree; - unsigned int nr, i; - - nr = DIV_ROUND_UP(nr_pages, ZSWAP_ADDRESS_SPACE_PAGES); - trees = kvzalloc_objs(*tree, nr); - if (!trees) { - pr_err("alloc failed, zswap disabled for swap type %d\n", type); - return -ENOMEM; - } - - for (i = 0; i < nr; i++) - xa_init(trees + i); - - nr_zswap_trees[type] = nr; - zswap_trees[type] = trees; - return 0; -} - -void zswap_swapoff(int type) -{ - struct xarray *trees = zswap_trees[type]; - unsigned int i; - - if (!trees) - return; - - /* try_to_unuse() invalidated all the entries already */ - for (i = 0; i < nr_zswap_trees[type]; i++) - WARN_ON_ONCE(!xa_empty(trees + i)); - - kvfree(trees); - nr_zswap_trees[type] = 0; - zswap_trees[type] = NULL; -} - /********************************* * debugfs functions **********************************/ -- 2.53.1-Meta