From patchwork Sun Feb 4 03:06:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chengming Zhou X-Patchwork-Id: 13544432 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 3DCC9C4828F for ; Sun, 4 Feb 2024 03:06:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A66AB6B007D; Sat, 3 Feb 2024 22:06:24 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A19E76B007E; Sat, 3 Feb 2024 22:06:24 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 906EA6B0080; Sat, 3 Feb 2024 22:06:24 -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 804996B007D for ; Sat, 3 Feb 2024 22:06:24 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 069801A0796 for ; Sun, 4 Feb 2024 03:06:24 +0000 (UTC) X-FDA: 81752633088.26.3444704 Received: from out-178.mta1.migadu.com (out-178.mta1.migadu.com [95.215.58.178]) by imf25.hostedemail.com (Postfix) with ESMTP id 48F23A0003 for ; Sun, 4 Feb 2024 03:06:21 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=none; spf=pass (imf25.hostedemail.com: domain of chengming.zhou@linux.dev designates 95.215.58.178 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=1707015981; 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=7MT4xJM6LG9fSA23gd9gvL3cDTs+SavPSKSov+Jw+Bs=; b=zj8lInWKPwW1iJ9J9kCeJKLUKnJss+00ai8i/Yv/8xuiN0bCWd96LJLFN94IoMdi8crY6d NI2t2NrXZzQbtWYW6iuaLjXgIGTOIsglGcTT4YeYHOQd/eKDEUQjPE1OCnZ6mrwb57mE/4 uDyqmbxnhm2jc2YciJChR8lkFjleEMM= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=none; spf=pass (imf25.hostedemail.com: domain of chengming.zhou@linux.dev designates 95.215.58.178 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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707015981; a=rsa-sha256; cv=none; b=678369NSP4DfeH6gfyXpjLeaiA2YC6z5WmBrzjs1wrcSwWcTHYLtdEdUHU9HwhB/u7TcTz TDp1NWV+beM2yJ1TDZcz6PhHGT1Eh2Phvmy5amQMijlSE03DCNKXvNr3w+YT6o53hNCPa8 hLDFIs6CvappTheHyesWJD8fZKuKVwY= 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:00 +0000 Subject: [PATCH v2 2/6] mm/zswap: invalidate zswap entry when swap entry free MIME-Version: 1.0 Message-Id: <20240201-b4-zswap-invalidate-entry-v2-2-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: 48F23A0003 X-Rspamd-Pre-Result: action=add header; module=dmarc; Action set by DMARC X-Rspam-User: X-Stat-Signature: g39ooh6w6kjht7kqm5n8ikd7fpyec5na X-Rspamd-Server: rspam01 X-Rspam: Yes X-HE-Tag: 1707015981-893331 X-HE-Meta: U2FsdGVkX1/SmdIT2/sMOKRDtTREXjdixaBVBDk+ALBMwuloXKCIYj2xI1rTF1EUJbc7DFfBtdbDbiW9VIIrDQ13Vq2t3Y3VpWjnq8y7i7hNDwcNzzyJY1dBdHBFuiZ0vrLVEUXVqIXRbsAEJvFW3XExCjXDfl//JVCrS/1TLD4XELR0gmp9l8njbYIP8zawSLvQ5+x5BPrZDpT01/xvzT6RittowVS6VxW4rVfdW1e/aLgFdNFq5jsk31RUkB0/tVeWZY2hYwKVJxMYoKH9klaLemwkHHnYswM6vzP/Tvsvo+/3UTGRz0HeTfKMKR8ExhngaV8LnhKFBphW08Mnf+uMHyecIn0XJoM9wcjGlz61gKHQTVBsWe/++NNaqBfkNfykUje7H2AdCROQA2AibFwsYKFilijLv3jAnSraMOP0GJVeXNKmmJN+k1FjJqOh+tMnHzZkpygmj+gXOmB4haiNVKYxo3jAHpZj/nCJX/vXsk++sWEAZGhFIcsrgJHO8IElKCBbooc/DfVgQkAEvqhyAaxDHPHMfy6VYiVfwV3EoXb0fffadNgB2x6OScNlFz4PR58yrM6El16qixtqL1FSMYlX4KqhF3ZcIvijJTq8HiGpV0Zaj5yGMuaiFiHHmm4a3rEI1df7LXNEo5tMlwBP1YPiUuo/mQexrGxhO5HRwVt6aPtxoyHUNbvx8PYQTvwb5FI0z/fMSdBKXBHWnDfgsPweHBtOegZELdznGzAcGGrslooTZvrA3vY6fjF9SUudl9hkToUOmCUH60/P0lQCi6FDnyYrzaV+hL+5mbmTtJkAQToB4TIjUxYD5kdSeokQnz32ba7Z5dL4QUPn0gpwbNZC+L3+FqR1PoDFXGbAFNH01MvdHQwTIll8UZ0QX3VcQokpBgP6+F1E54A9n8U5i5HyRYmTXU3Llqih3dLylEIOIVwooMDtl1ncnJCm4ZDJFo3RbKJbdRlKzv9 8RLaOJjc Mhu9AgYYfuOa50aazfpcK/GyNqn4jvuAtEaMjlzWWll9ZnZ8= 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: During testing I found there are some times the zswap_writeback_entry() return -ENOMEM, which is not we expected: bpftrace -e 'kr:zswap_writeback_entry {@[(int32)retval]=count()}' @[-12]: 1563 @[0]: 277221 The reason is that __read_swap_cache_async() return NULL because swapcache_prepare() failed. The reason is that we won't invalidate zswap entry when swap entry freed to the per-cpu pool, these zswap entries are still on the zswap tree and lru list. This patch moves the invalidation ahead to when swap entry freed to the per-cpu pool, since there is no any benefit to leave trashy zswap entry on the tree and lru list. With this patch: bpftrace -e 'kr:zswap_writeback_entry {@[(int32)retval]=count()}' @[0]: 259744 Note: large folio can't have zswap entry for now, so don't bother to add zswap entry invalidation in the large folio swap free path. Reviewed-by: Nhat Pham Acked-by: Johannes Weiner Signed-off-by: Chengming Zhou Acked-by: Yosry Ahmed --- include/linux/zswap.h | 4 ++-- mm/swap_slots.c | 3 +++ mm/swapfile.c | 1 - mm/zswap.c | 5 +++-- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/include/linux/zswap.h b/include/linux/zswap.h index 91895ce1fdbc..341aea490070 100644 --- a/include/linux/zswap.h +++ b/include/linux/zswap.h @@ -29,7 +29,7 @@ struct zswap_lruvec_state { bool zswap_store(struct folio *folio); bool zswap_load(struct folio *folio); -void zswap_invalidate(int type, pgoff_t offset); +void zswap_invalidate(swp_entry_t swp); int zswap_swapon(int type, unsigned long nr_pages); void zswap_swapoff(int type); void zswap_memcg_offline_cleanup(struct mem_cgroup *memcg); @@ -50,7 +50,7 @@ static inline bool zswap_load(struct folio *folio) return false; } -static inline void zswap_invalidate(int type, pgoff_t offset) {} +static inline void zswap_invalidate(swp_entry_t swp) {} static inline int zswap_swapon(int type, unsigned long nr_pages) { return 0; diff --git a/mm/swap_slots.c b/mm/swap_slots.c index 0bec1f705f8e..90973ce7881d 100644 --- a/mm/swap_slots.c +++ b/mm/swap_slots.c @@ -273,6 +273,9 @@ void free_swap_slot(swp_entry_t entry) { struct swap_slots_cache *cache; + /* Large folio swap slot is not covered. */ + zswap_invalidate(entry); + cache = raw_cpu_ptr(&swp_slots); if (likely(use_swap_slot_cache && cache->slots_ret)) { spin_lock_irq(&cache->free_lock); diff --git a/mm/swapfile.c b/mm/swapfile.c index 0580bb3e34d7..65b49db89b36 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -744,7 +744,6 @@ static void swap_range_free(struct swap_info_struct *si, unsigned long offset, swap_slot_free_notify = NULL; while (offset <= end) { arch_swap_invalidate_page(si->type, offset); - zswap_invalidate(si->type, offset); if (swap_slot_free_notify) swap_slot_free_notify(si->bdev, offset); offset++; diff --git a/mm/zswap.c b/mm/zswap.c index 735f1a6ef336..d8bb0e06e2b0 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -1738,9 +1738,10 @@ bool zswap_load(struct folio *folio) return true; } -void zswap_invalidate(int type, pgoff_t offset) +void zswap_invalidate(swp_entry_t swp) { - struct zswap_tree *tree = swap_zswap_tree(swp_entry(type, offset)); + pgoff_t offset = swp_offset(swp); + struct zswap_tree *tree = swap_zswap_tree(swp); struct zswap_entry *entry; spin_lock(&tree->lock);