From patchwork Thu Feb 1 15:49:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chengming Zhou X-Patchwork-Id: 13541308 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 3D470C4828F for ; Thu, 1 Feb 2024 15:50:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C353E6B009A; Thu, 1 Feb 2024 10:50:26 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B70336B009B; Thu, 1 Feb 2024 10:50:26 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A11506B009C; Thu, 1 Feb 2024 10:50:26 -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 8B2076B009A for ; Thu, 1 Feb 2024 10:50:26 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 634761409E3 for ; Thu, 1 Feb 2024 15:50:26 +0000 (UTC) X-FDA: 81743672052.28.451D919 Received: from out-175.mta1.migadu.com (out-175.mta1.migadu.com [95.215.58.175]) by imf22.hostedemail.com (Postfix) with ESMTP id 89C04C0024 for ; Thu, 1 Feb 2024 15:50:24 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=none; spf=pass (imf22.hostedemail.com: domain of chengming.zhou@linux.dev designates 95.215.58.175 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=1706802624; 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=KvE903wmurJBYqeBdkfmIVsoeRCjMmKspY25ZMwQskg=; b=Prpg00mQdBQkHE8QH2gj4b9MokNL2kTvPvysrPM2/R0dolBeMrtddLdA2fmTUgGSVwMZIW 6nEL+aPj1JPLU/jfOq0Y8q+yTOiXCyYa5QXG3ps9i6t9ttyKnZC6nQd5P0JDzeghCL7C6h CiZEJ8e1Yw+RI6188mx+5D75Q7tGWGc= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706802624; a=rsa-sha256; cv=none; b=fwtOhN184WpldTkSMbzFDpEjrt0ECvOn9h/7/yj9PcePvuDU/LqFBlrgM7bRa6J0ZNjhAa QlRMuw1B/GZdz73NhxNikYtjePXL3IvFmP6VL+E83l8AgC7jccJNVZ7TlJDaBciS7U5sbm vMZEjpVtIqfRj6P0rlv81GZuDuP7M5Y= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=none; spf=pass (imf22.hostedemail.com: domain of chengming.zhou@linux.dev designates 95.215.58.175 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:02 +0000 Subject: [PATCH 2/6] mm/zswap: invalidate zswap entry when swap entry free MIME-Version: 1.0 Message-Id: <20240201-b4-zswap-invalidate-entry-v1-2-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: 89C04C0024 X-Rspamd-Server: rspam11 X-Stat-Signature: fj5fsij66hm5yfpsbfwge7tfinud9ukk X-Rspam: Yes X-HE-Tag: 1706802624-327872 X-HE-Meta: U2FsdGVkX19HMPdrSSfbvPrOg8HMVIqM3sq942MdDZw8vNq0xE9k8Wv1vhZHV630yBLrJu+w7XUE50qlKCc1ZunN9ucMPYp8ByWRYqw7zFh0SvzQ20Po2I2xrCUvSD/SKx29ElgdKa0IWyJ0IBy50dC0h3dY4mr6HDS2ISoNIhaJq1EDVDDz47sNZJ95wFBjkMD6hV+XzD3qvNYqubV+or2U5XOSUFH1HJTwvUvo0B4JSWDU3XXXEMq7Tx7TC71Z4dBFKTq0RjvhCWBizrgBIjg64aIgkCIlJvwKknqK2IfxTpxc9Au5hllIBN9ceXj6Kw0QTsGeFvHhFZ8qvRSFVevUvDIt3gqigUoQt7VgqvTgx8afPVTILQh809LJhUb51pAGHYdXDFE3Kic2jlwImiYzI4pUkwi8NkZsG12iEaUl2oAo+KpJeVAlSIi3jhFKBLe5GzfA1pzk1raupZ0Lhnvp/6aOFawGgQR84ZUe3kEZuYoXlTXyoZWymPi7s2ji9Hf2IZr+xhoeJ/MPoxLNtAXy3W4GuLMyt+z28IQwxw6neJEsuE3IbOOuiOVbokoQ1JwjYMv/dLgw61xt7s90wjmBlO42yBaQh+4GJUXVIQw2hJjnvZgTEYJ37RAtgfW2oIk7E/FeNoVUmyATJ7p7FfxEirhw2yBdSO4q8oTL5ByTEVS5M/gkxQtbXKE5ncLFdfEi31uthmShNrzVo7DzoRRmnzL53DIxBdzsaCmKnJ0eAfsre6ixluhfjb+OpAroXFH2hY5XB/kCEMkQgcxtu4RpIcFAXEXVA2sBU45y8RYZ7wi4Zh5Lc0GQNcZ7AxYDAgbJi+v//CMfhqxrpQz8eGxQuo525VZTU3hJcF7DWBFb3p39CKTq4PBQGd8QnQgIPwfo99iAG7m0195VLTY1vGKxiWCIYurGD+INihgBwxZNNpxZvbLN1VBQMJ8GIQ2d00P/nEpUxQ47Eq0twnA tNPfeQkL CQazUMun2PpkRfBBfuhmsNqOWgQ3eCYguZiUiMEoUH9ADVtesJiI6hLJDRBFoTURh18Bp6QMWpRXy/Ossjx+6lXfNYbFizFmYfp7BXrYtxJ0yY18xyden6JxI3e6C+5IX2fQrMhxIX2KxgRWzqMFTd01leeUmnlVpb5XBJiD5R9cMjsx3sarC870IEGYT9lnqbxsE7tQyIpf1GSx8Pga4MD17x+5F/iYXPettELocrC2a5zfsjFaOlA5N4WwM+ujXYwmKKqulbeK6rBSi38JmVYHnE2nhc0yIlzbgo+vicEtcmZ9TwmH+3Pgts0JKf8FzQYmYpFC1WWTTnw99p6xRifs7QfxeRp2rQikg1CWsAkEBxrg= 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. Signed-off-by: Chengming Zhou Acked-by: Johannes Weiner Reviewed-by: Nhat Pham --- include/linux/zswap.h | 4 ++-- mm/swap_slots.c | 2 ++ mm/swapfile.c | 1 - mm/zswap.c | 5 +++-- 4 files changed, 7 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..d24cdea26daa 100644 --- a/mm/swap_slots.c +++ b/mm/swap_slots.c @@ -273,6 +273,8 @@ void free_swap_slot(swp_entry_t entry) { struct swap_slots_cache *cache; + 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);