From patchwork Sun Feb 4 03:06:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chengming Zhou X-Patchwork-Id: 13544436 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7D03EC4828D for ; Sun, 4 Feb 2024 03:06:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ED8DD6B0087; Sat, 3 Feb 2024 22:06:30 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E36F46B0088; Sat, 3 Feb 2024 22:06:30 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BC57E6B0089; Sat, 3 Feb 2024 22:06:30 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id A0E4E6B0087 for ; Sat, 3 Feb 2024 22:06:30 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 6217480151 for ; Sun, 4 Feb 2024 03:06:30 +0000 (UTC) X-FDA: 81752633340.25.D8A6B82 Received: from out-188.mta1.migadu.com (out-188.mta1.migadu.com [95.215.58.188]) by imf25.hostedemail.com (Postfix) with ESMTP id 83D3BA0007 for ; Sun, 4 Feb 2024 03:06:28 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=none; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=bytedance.com (policy=quarantine); spf=pass (imf25.hostedemail.com: domain of chengming.zhou@linux.dev designates 95.215.58.188 as permitted sender) smtp.mailfrom=chengming.zhou@linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1707015988; 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; bh=CpJJ+lmut35Sgt5qzijPizjCkrhWKWh5X/lhHB6j0D8=; b=Izk/QUd/52PFZesujlctVd+IHmbJk8wOEWGiRt7Lx68Z9cEy94c4WuXUKZxaSXECfwjPWa +RD6l1Xqa1X6zo6Em2jn0EtImR1iKz0bwBG1hbVlo+jzdnOyYBmJzcxvD4StrNgMMTWW7n c4bao0jAplFizzpY4gnWRF/keBRHS2g= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=none; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=bytedance.com (policy=quarantine); spf=pass (imf25.hostedemail.com: domain of chengming.zhou@linux.dev designates 95.215.58.188 as permitted sender) smtp.mailfrom=chengming.zhou@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707015988; a=rsa-sha256; cv=none; b=zQ+fST17APbhN7bMAhkvjfCD1pxvFWtSTB5GJfshgD9UACc5YV13Q0deH/78kLQ+Ndfdx6 vr3QQxkMMKJcPO5xRO1XV2dhAXo0Svt/U4B3rg1UBlJVXveAXWVP4NqyZdHNlbUgZ71b0E 6kGz9nZJznxARXyybBn99z3qtqy8PL0= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Chengming Zhou Date: Sun, 04 Feb 2024 03:06:04 +0000 Subject: [PATCH v2 6/6] mm/zswap: zswap entry doesn't need refcount anymore MIME-Version: 1.0 Message-Id: <20240201-b4-zswap-invalidate-entry-v2-6-99d4084260a0@bytedance.com> References: <20240201-b4-zswap-invalidate-entry-v2-0-99d4084260a0@bytedance.com> In-Reply-To: <20240201-b4-zswap-invalidate-entry-v2-0-99d4084260a0@bytedance.com> To: Nhat Pham , Yosry Ahmed , Andrew Morton , Johannes Weiner Cc: linux-mm@kvack.org, Nhat Pham , Chengming Zhou , linux-kernel@vger.kernel.org, Yosry Ahmed , Johannes Weiner X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 83D3BA0007 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: o4j81th4ytxcou3fsezsebq9pqeacccf X-Rspamd-Pre-Result: action=add header; module=dmarc; Action set by DMARC X-Rspam: Yes X-HE-Tag: 1707015988-957758 X-HE-Meta: U2FsdGVkX19Dz6mBu7MAwBh/nsrBq6dB5IADrTK73jjHnUX1o55E4UeGSjraANHcFWlneOTM4ZbXmMxHv5zhPiXieNvoUHJho8DtIoodwmhqXVB6VMu8PvtcqucPJFe5Lx/2WMV9rGxfi4wlehovh1fbuSf4inVu8sufQX+JrX8K9gSFBZsg2dqzRNUCFq8QLLudsrO+Lo6o/Jow6+oP44sK8DXGLQsLC32jok09j/2mG0CQ2+Bnb5IW/07USZNuhThYJ1XtXcOvaB/fCt7x2ETM92UmAi817RsCQDUzkLJbRtuz4rPkrzGDqsHvKSfD3kfGeuWafNUb4f605K6JE1CqMn1Q+v9wf+IcPpG9pDNfXlEa0+AT6JaXyxHNQ6LpN2cVOTyow3IOiu338yUV6gUjTzHBA2luhWQ1Thxo4rpVtuGWfuRlWPM7EOXKErcHqLVSlFzSogroAGu03fwm2AnT7vF9K/L+Ckam2AkQ6DJx7KKwLGEc5DK4fRR1yu4Np65f5U0TVHPFUHUOExsBWcjUPB/8NHD2bVFrh2nNnlcLySSdcISPYf0Hj/OJdbPBlqLZw32wjIlo3snwS59jnQatGJlORyZ0Y2JUTyxu+VoSlYsxOiVidHGatdbV5ImMqsJmjrM0mq/yj7DiUt7ECIJrhMdggeU6dyfj4hAYNRagOf0QwBjN7pz1668FfuvNQVDYs8U9GxqQQt7dN2PHPSErcUF6dXaIwPXJRh2Ox7argkNqJA5QvwUrjccJeiJRn8XqVDbYzUzcYq3blC0tNLn743/zGMNVLTH17UFjhFVmYEHPWkzJW3y/zAp2tCNVYTEAoTByaAVsq9lVkENnz++DtBoCovRnyn/BZawhfqnm8iHPy37K25AAW8thf+flKSPtK574Rlidtk2i6dCl0/gogN254hgMz8v6EGPeqX9qCEzlAUX0hNTnvNoFVmOEIztHqnL6wAo1QGkmDic 4cQYo8Dy nsIlFpaBQoBJtnRGLlyHH7c0WQSvIrZK7GrUGh+HXd6t2MxosqnjaRHXsR8Ge7x4iKkmHVUMqgMLlsrw8UT6iataKXjwVc7QyRuFXPAutmDuthjsia2Bs1GlcNZ2UV/VyXy9Vx6ol1XCxIBeecEQK1n2J8JkW9EhqeX8TIghKaDUAf/sB4abjEADSNroAD2i/2Yo5wJI+HVlbrkFfqu+s0c22GI4cCAHBy8eJrNkTaaViJrV+vBVydrPQS0S4ZW65kgw00GhKm47+4xy5bgSTChQT/WJdfcpjFU4Pyjdw1cuHJm19yKiWvrasziZk8w0T24z5F0ryLKNCez2AbhVPkEA+pBSvjFTMxffvJtnPnw0qViFjVeSpD8dce4VG5397bDVpv4+bNTUOaTizw7eaTgayPvCfQy9PJ1AggoEuX0e4s9U73PvXfpUgfQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Since we don't need to leave zswap entry on the zswap tree anymore, we should remove it from tree once we find it from the tree. Then after using it, we can directly free it, no concurrent path can find it from tree. Only the shrinker can see it from lru list, which will also double check under tree lock, so no race problem. So we don't need refcount in zswap entry anymore and don't need to take the spinlock for the second time to invalidate it. The side effect is that zswap_entry_free() maybe not happen in tree spinlock, but it's ok since nothing need to be protected by the lock. Reviewed-by: Nhat Pham Acked-by: Johannes Weiner Signed-off-by: Chengming Zhou --- mm/zswap.c | 63 +++++++++++--------------------------------------------------- 1 file changed, 11 insertions(+), 52 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index cbf379abb6c7..cd67f7f6b302 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -193,12 +193,6 @@ struct zswap_pool { * * rbnode - links the entry into red-black tree for the appropriate swap type * swpentry - associated swap entry, the offset indexes into the red-black tree - * refcount - the number of outstanding reference to the entry. This is needed - * to protect against premature freeing of the entry by code - * concurrent calls to load, invalidate, and writeback. The lock - * for the zswap_tree structure that contains the entry must - * be held while changing the refcount. Since the lock must - * be held, there is no reason to also make refcount atomic. * length - the length in bytes of the compressed page data. Needed during * decompression. For a same value filled page length is 0, and both * pool and lru are invalid and must be ignored. @@ -211,7 +205,6 @@ struct zswap_pool { struct zswap_entry { struct rb_node rbnode; swp_entry_t swpentry; - int refcount; unsigned int length; struct zswap_pool *pool; union { @@ -222,11 +215,6 @@ struct zswap_entry { struct list_head lru; }; -/* - * The tree lock in the zswap_tree struct protects a few things: - * - the rbtree - * - the refcount field of each entry in the tree - */ struct zswap_tree { struct rb_root rbroot; spinlock_t lock; @@ -890,14 +878,10 @@ static int zswap_rb_insert(struct rb_root *root, struct zswap_entry *entry, return 0; } -static bool zswap_rb_erase(struct rb_root *root, struct zswap_entry *entry) +static void zswap_rb_erase(struct rb_root *root, struct zswap_entry *entry) { - if (!RB_EMPTY_NODE(&entry->rbnode)) { - rb_erase(&entry->rbnode, root); - RB_CLEAR_NODE(&entry->rbnode); - return true; - } - return false; + rb_erase(&entry->rbnode, root); + RB_CLEAR_NODE(&entry->rbnode); } /********************************* @@ -911,7 +895,6 @@ static struct zswap_entry *zswap_entry_cache_alloc(gfp_t gfp, int nid) entry = kmem_cache_alloc_node(zswap_entry_cache, gfp, nid); if (!entry) return NULL; - entry->refcount = 1; RB_CLEAR_NODE(&entry->rbnode); return entry; } @@ -954,33 +937,15 @@ static void zswap_entry_free(struct zswap_entry *entry) zswap_update_total_size(); } -/* caller must hold the tree lock */ -static void zswap_entry_get(struct zswap_entry *entry) -{ - WARN_ON_ONCE(!entry->refcount); - entry->refcount++; -} - -/* caller must hold the tree lock */ -static void zswap_entry_put(struct zswap_entry *entry) -{ - WARN_ON_ONCE(!entry->refcount); - if (--entry->refcount == 0) { - WARN_ON_ONCE(!RB_EMPTY_NODE(&entry->rbnode)); - zswap_entry_free(entry); - } -} - /* - * If the entry is still valid in the tree, drop the initial ref and remove it - * from the tree. This function must be called with an additional ref held, - * otherwise it may race with another invalidation freeing the entry. + * The caller hold the tree lock and search the entry from the tree, + * so it must be on the tree, remove it from the tree and free it. */ static void zswap_invalidate_entry(struct zswap_tree *tree, struct zswap_entry *entry) { - if (zswap_rb_erase(&tree->rbroot, entry)) - zswap_entry_put(entry); + zswap_rb_erase(&tree->rbroot, entry); + zswap_entry_free(entry); } /********************************* @@ -1219,7 +1184,7 @@ static int zswap_writeback_entry(struct zswap_entry *entry, } /* Safe to deref entry after the entry is verified above. */ - zswap_entry_get(entry); + zswap_rb_erase(&tree->rbroot, entry); spin_unlock(&tree->lock); zswap_decompress(entry, &folio->page); @@ -1228,10 +1193,7 @@ static int zswap_writeback_entry(struct zswap_entry *entry, if (entry->objcg) count_objcg_event(entry->objcg, ZSWPWB); - spin_lock(&tree->lock); - zswap_invalidate_entry(tree, entry); - zswap_entry_put(entry); - spin_unlock(&tree->lock); + zswap_entry_free(entry); /* folio is up to date */ folio_mark_uptodate(folio); @@ -1702,7 +1664,7 @@ bool zswap_load(struct folio *folio) spin_unlock(&tree->lock); return false; } - zswap_entry_get(entry); + zswap_rb_erase(&tree->rbroot, entry); spin_unlock(&tree->lock); if (entry->length) @@ -1717,10 +1679,7 @@ bool zswap_load(struct folio *folio) if (entry->objcg) count_objcg_event(entry->objcg, ZSWPIN); - spin_lock(&tree->lock); - zswap_invalidate_entry(tree, entry); - zswap_entry_put(entry); - spin_unlock(&tree->lock); + zswap_entry_free(entry); folio_mark_dirty(folio);