From patchwork Thu Feb 1 15:49:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chengming Zhou X-Patchwork-Id: 13541312 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 EAF21C4828F for ; Thu, 1 Feb 2024 15:50:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7C8936B0098; Thu, 1 Feb 2024 10:50:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7525B6B009E; Thu, 1 Feb 2024 10:50:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5CB276B00A0; Thu, 1 Feb 2024 10:50:44 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 45ACE6B009E for ; Thu, 1 Feb 2024 10:50:44 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 044AD1A0E60 for ; Thu, 1 Feb 2024 15:50:43 +0000 (UTC) X-FDA: 81743672808.29.55542E6 Received: from out-171.mta1.migadu.com (out-171.mta1.migadu.com [95.215.58.171]) by imf01.hostedemail.com (Postfix) with ESMTP id 1CCED4000F for ; Thu, 1 Feb 2024 15:50:41 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=none; spf=pass (imf01.hostedemail.com: domain of chengming.zhou@linux.dev designates 95.215.58.171 as permitted sender) smtp.mailfrom=chengming.zhou@linux.dev; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=bytedance.com (policy=quarantine) ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706802642; 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=J0ZWT+pBrGoRn+2Kb2hwCOno34Uzt8vCj8esbw8lBgQ=; b=5h7F7Dq0lsoSi9zMN9A0k14d8d8DDdGuk9xLZxLIBB5IscmMNK9ADy3TqsLUpQnJa93A8C +VP+oOnv9P+3e98+xxNWMiAjTJNRlx7d6qWwYyY9sJ5Jzk75xFLMZ3ZW4l2W8I95/SPXAC hJGAs9hkM1O2FA6VrDb01uCSkoj955E= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706802642; a=rsa-sha256; cv=none; b=j9TD8Jf5oCRDxLWwzUTDJqhF5rne7oq91txqhm8cmRVYKMqemOZLI4MnN16Cx6bqu444B/ fmkwEB4wP2+iMKMLRkL0M4uAB70k2U7iQl3uJXFEr0Y97WPTD1Nx0E1xA90S5ek8CS5PDf y44Fq1A0ugkJZArssuufmaum0Zfswc4= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=none; spf=pass (imf01.hostedemail.com: domain of chengming.zhou@linux.dev designates 95.215.58.171 as permitted sender) smtp.mailfrom=chengming.zhou@linux.dev; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=bytedance.com (policy=quarantine) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Chengming Zhou Date: Thu, 01 Feb 2024 15:49:06 +0000 Subject: [PATCH 6/6] mm/zswap: zswap entry doesn't need refcount anymore MIME-Version: 1.0 Message-Id: <20240201-b4-zswap-invalidate-entry-v1-6-56ed496b6e55@bytedance.com> References: <20240201-b4-zswap-invalidate-entry-v1-0-56ed496b6e55@bytedance.com> In-Reply-To: <20240201-b4-zswap-invalidate-entry-v1-0-56ed496b6e55@bytedance.com> To: Nhat Pham , Johannes Weiner , Andrew Morton , Yosry Ahmed Cc: linux-kernel@vger.kernel.org, Yosry Ahmed , Chengming Zhou , Johannes Weiner , linux-mm@kvack.org X-Migadu-Flow: FLOW_OUT X-Rspamd-Pre-Result: action=add header; module=dmarc; Action set by DMARC X-Rspam-User: X-Rspamd-Queue-Id: 1CCED4000F X-Rspamd-Server: rspam11 X-Stat-Signature: 4ekpokstixm787rysgt35kq4qhnm157y X-Rspam: Yes X-HE-Tag: 1706802641-473290 X-HE-Meta: U2FsdGVkX19JwD8TZvTHZITSmHIb9TvMPLs8Y9rFUTCqmqQiZDy0C8jcA/ShYXCxjAH/okaSt5Z3is7oelhKqyFa+O+8OwZF8zDzLTVYS7IeGlwiqYjKWnr03yfaCU81kCi8WeXruZq3kF/GSRyBtiVnKgkn3lSbgOjykpPvK2Os/enJ1Sj882qzSJdu6TI6/EsznKT894OIFIbWtDzAoKVfOH+npiln4ZEvELgHyCA8Jw0f1W32mTH6YIUO6dWBJh+TkAMK6U8OpTBy5czwknxSTBIIBUIOt/Veqrq/ueVmwIOsZUfPoWi98tKi0zYF4VfftCqRfsKo9cEdWw1CdKYWhvOBAoIdn3esl1c3lZOaXnnVzzYGPqZjbeoXLKsN4gb1/P403LiEn2SeHCxT+Y7XBWw4poFlKfFZQjbHT+1+rfmFJYsECJ/l5+zcklhlR/xukPq/t8C0xmnUvtenIrOEriRVOdEG0M5V2s9eD151wsUGHDbsQMhmyw/fmKj7JOyh1xetJ7VD0LpbjU+fYzpaX0DQ2Ul52hKABxgpReCdCRMf2x955CPhkmzpiBbuGhOCu/sjw0PqCL+2cAKk1G6ljkwM8reHBO01G/cEf4tVcH0OWlaG1pL1fFxF3QFAdcj3R/dTBqBDYKYpLjXeDv8YgabTYuyrR7uKGK/MdGzx7WpqPiEWjKL0NyFRTNrTcfWL/dHOATCLVIx74UxHYf88YSUePVBoSHOam3Wnz7Ub2lMCrtguKl9FlD/vEsLe7sNtjgvCb2XIu03VtIMCnNAFyhyee43SkUaSD0SUms26Uyhf58RxZax0KuofSAjE5mNB6JChQTxWDO7HEp7Pst2LJ9Psi0A+rDtqucP6MVpRV/+VlfurEpWSorzjSv5qPPB92wI6QIq7mWIPihUVlOAyo1oTqHjMdjG9929C6iACWCoLLDnablM9p/wdgaWH+LHVhGvgzC7IuLnc9hD QsYLaggt KGp0mfuMOQAzlASU5nL5YpOAAM5uyKx03iqLiv3UoTi6A2GMBkPrSWnjj24TUlBvueBIvG6XGTlbXpIAKxF3db79+qBYrN8NZG7VrBStIMZ5QJjNjZnnjE9BSg+O/bOSdhtKkw7HvRjB1mID44qyInH4983mJS963xAHFklgIMqkD4ZZHu/NcVA2jW9D+NfTr4fFkBN7JHcZh04pEkfpY3+QGYahQCySpYFueXFuAwNYazQS/WP2OkXUN0hiMCwsVJt7EcKP4RKv5xG/oPf+F4SVwMdtxTnGNXvki4+uzRw8iN/Po/IYjzoLkM62US7WSmz3YlCXTI6ox8IreaEyi6meAAt1p4CfcI3tembkwsKbi6IA= 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. Signed-off-by: Chengming Zhou Acked-by: Johannes Weiner Reviewed-by: Nhat Pham --- 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);