From patchwork Wed Dec 6 09:46:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chengming Zhou X-Patchwork-Id: 13481318 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 A9F68C10DC1 for ; Wed, 6 Dec 2023 09:46:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 311656B00A0; Wed, 6 Dec 2023 04:46:37 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2C1EB6B00A1; Wed, 6 Dec 2023 04:46:37 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 115896B00A2; Wed, 6 Dec 2023 04:46:37 -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 03C966B00A0 for ; Wed, 6 Dec 2023 04:46:37 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id D13FE4017F for ; Wed, 6 Dec 2023 09:46:36 +0000 (UTC) X-FDA: 81535913592.13.3F5BEAB Received: from out-171.mta1.migadu.com (out-171.mta1.migadu.com [95.215.58.171]) by imf28.hostedemail.com (Postfix) with ESMTP id E3B57C001D for ; Wed, 6 Dec 2023 09:46:34 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=none; spf=pass (imf28.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=1701855995; 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=jkKMup39apxbLnzkAyTXUI4I35+uiNZ5r/9DdtLwYPQ=; b=DcsEUihx/zXQgJfnJzvb9779biNyBZ9tTvJEHWJr13CC5BPbxde5e4pCM9LtohHL7aB6ch SUwEdYnLbSQdalG+492TwqvFt5/GVvREiBDwHDOr5lE5xCjajTOoxF333QTlNKPp29dL39 wT+4cm4A20XKbHIFic1D093kKG4wNuk= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=none; spf=pass (imf28.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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1701855995; a=rsa-sha256; cv=none; b=TMuGHhzbC3QK+9/Zo1K4XMJdBtWjmgUTS2SvjPguqLaPJLOSDIgo96HCh4DQlixDQ9Q4ZZ s8Mr2x2hCtHs7hCXPQVL0l6jM+17UOy6Ig/0dypA+JPlRgSxi9LP/v0Dzr/HcWghNi0YpY j59Qku56nQox/Ia/OCNVBZXteKC+aLU= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Chengming Zhou Date: Wed, 06 Dec 2023 09:46:24 +0000 Subject: [PATCH 1/7] mm/zswap: make sure each swapfile always have zswap rb-tree MIME-Version: 1.0 Message-Id: <20231206-zswap-lock-optimize-v1-1-e25b059f9c3a@bytedance.com> References: <20231206-zswap-lock-optimize-v1-0-e25b059f9c3a@bytedance.com> In-Reply-To: <20231206-zswap-lock-optimize-v1-0-e25b059f9c3a@bytedance.com> To: Vitaly Wool , Nhat Pham , Johannes Weiner , Michal Hocko , Seth Jennings , Dan Streetman , Andrew Morton , Yosry Ahmed Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Chengming Zhou X-Developer-Signature: v=1; a=ed25519-sha256; t=1701855988; l=3698; i=zhouchengming@bytedance.com; s=20231204; h=from:subject:message-id; bh=PZN0xLXpa6FJXo43SkR7sRqIVKPQ5jd6uVBrSVx9EAE=; b=/IE8MsA85GAZ6MZCk9fTWWd8zckSLavOzPjJFd5hfxDZQxaW88TjMxNyw9krvi0fXcdtJlBTb 6fS45gqAMpsAiMJo+PpOcT2QfWAipRf/YXZ1gMVtSGgZ7M7zveNY0XP X-Developer-Key: i=zhouchengming@bytedance.com; a=ed25519; pk=xFTmRtMG3vELGJBUiml7OYNdM393WOMv0iWWeQEVVdA= X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: E3B57C001D X-Rspamd-Pre-Result: action=add header; module=dmarc; Action set by DMARC X-Rspam-User: X-Stat-Signature: i6tj5omsyi7d6o9rotxyneft1bi9bewo X-Rspamd-Server: rspam01 X-Rspam: Yes X-HE-Tag: 1701855994-729064 X-HE-Meta: U2FsdGVkX1/D2uPe4x0iRkAK6c+NfFvF13DFKl8FRVRRzQxjSP8K147NW1SSkvOV+y21IPi1RKZHslfoQAsqJq/mWRjDG/4Sr8UdQoRm7A7hV1riyygVuc5YdtbDtv8bupqbzhRb4m8PhikpOniSlx9rO2sDjpx2nY4ayrdsCW6UGl5c/RybNXJrvDCL0W87g1l8tKlVviWFc4Zyg2CPIXvmandcfE9FEFpBM6EMcxECDSo23XdBJAQVwHrDdY5AL+2x/xMWWzh2Oi/6IrUDHNH8sPowg8cjzUfVbfOdPMA2rp+DmZ/6Ca4m7YS9UcCa5udh2OHKsZSexBKQL/s8bGXAbWR94cvjSeYd1pEv3fXTkLPyEVgoaYI1+kuWsdqy4trBiWWpQyRiNNJxZkdS2nWF6pAV9GDpf5/TrsmrPUVB5qZXQ9ucgSo84lz/233VYRwo85vyqFNyKa6K7YAYt/qIy0A1HeqAA56SHbMSWqeQrI56vAEM4RI56AsnJDoe9R3zC7Bm+0HfP10yR4ZIBt1VtX7BfSdFGDC9bh0Wjq2gxneM5VgyyAU12vzLUQEa42PazP9/9ANgdhlt0iDSUtWySwt04ds9k0gunl44ntDUTds61HnJHe1O0HAYatQIF5KBZQkwVRY8mEdvZgD+kWckIy916OH42/UfDTKACkd0pr6JTMkZpM279tjxoLKeNjOrpKOL0pSqKbR2W/WzbWb/Gcva9hk5MOdfYvz87yrq/cb5hsvvaCcUmwp+P8kjFW1IoReFVZ6aKfD94NDil/v77ER9w96fZtL+JAGgol1TFHx98z/xWxyV3pgmGqPBhP+zkmdMB/d1yBnPzs6pk1w0F/f5KMf3r3JWtqaThh5NXv/nSIW2N43HBPxfNeNQUSTxFkd0vTVNYcGYaovkDY5WrKO5WFcDOhdFK86m/wsAIiiczQR6S7pmUH+ssXSdzK0TcMy5kQ2z6kw4VhT qA8ZdvcK oa7jymU1X5y6zc+oYT/ceyoDugw2XEiC3hW0iKXkudNlCgSijgdbdM6ULW4e/KfoKLBoHCyXMgvlvgd11nHt1t8DKt5Gf5EDAcunKJ++6K7UzeuoKLACl+P8KiWAgJGDe2dUqHVa/RuOs0kuiAxlOQ8Y2Z+lQ451exuPmud4zy4JMMGATahvmwOGTswzoPh6UlPX38RSX7QNl46K2h8d2AGKB5YWCtfqgB0WC5P6rBUIqM+gfLKklJMY9aq2F6O2/UOtTgjMkitFpiWLWvZtz+AUDwux/9gOTXiurilkbczK6ATv5TGsT/k2pSnWsTsPaoQdigGpneyuNRIM4L7dPxFvcLr7voggHxhNY8MBfWe21YwWTBBvlOmoWag== 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: Not all zswap interfaces can handle the absence of the zswap rb-tree, actually only zswap_store() has handled it for now. To make things simple, we make sure each swapfile always have the zswap rb-tree prepared before being enabled and used. The preparation is unlikely to fail in practice, this patch just make it explicit. Signed-off-by: Chengming Zhou --- include/linux/zswap.h | 4 ++-- mm/swapfile.c | 10 +++++++--- mm/zswap.c | 7 ++++--- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/include/linux/zswap.h b/include/linux/zswap.h index 08c240e16a01..7cccc02cb9e9 100644 --- a/include/linux/zswap.h +++ b/include/linux/zswap.h @@ -30,7 +30,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_swapon(int type); +int zswap_swapon(int type); void zswap_swapoff(int type); void zswap_memcg_offline_cleanup(struct mem_cgroup *memcg); void zswap_lruvec_state_init(struct lruvec *lruvec); @@ -50,7 +50,7 @@ static inline bool zswap_load(struct folio *folio) } static inline void zswap_invalidate(int type, pgoff_t offset) {} -static inline void zswap_swapon(int type) {} +static inline int zswap_swapon(int type) {} 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) {} diff --git a/mm/swapfile.c b/mm/swapfile.c index 8be70912e298..939e7590feda 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -2346,8 +2346,6 @@ static void enable_swap_info(struct swap_info_struct *p, int prio, unsigned char *swap_map, struct swap_cluster_info *cluster_info) { - zswap_swapon(p->type); - spin_lock(&swap_lock); spin_lock(&p->lock); setup_swap_info(p, prio, swap_map, cluster_info); @@ -3165,6 +3163,10 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) if (error) goto bad_swap_unlock_inode; + error = zswap_swapon(p->type); + if (error) + goto free_swap_address_space; + /* * Flush any pending IO and dirty mappings before we start using this * swap device. @@ -3173,7 +3175,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_address_space; + goto free_swap_zswap; } mutex_lock(&swapon_mutex); @@ -3197,6 +3199,8 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) error = 0; goto out; +free_swap_zswap: + zswap_swapoff(p->type); free_swap_address_space: exit_swap_address_space(p->type); bad_swap_unlock_inode: diff --git a/mm/zswap.c b/mm/zswap.c index 0f086ffd7b6a..5e2b8d5ee33b 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -1560,7 +1560,7 @@ bool zswap_store(struct folio *folio) if (folio_test_large(folio)) return false; - if (!zswap_enabled || !tree) + if (!zswap_enabled) return false; /* @@ -1850,19 +1850,20 @@ void zswap_invalidate(int type, pgoff_t offset) spin_unlock(&tree->lock); } -void zswap_swapon(int type) +int zswap_swapon(int type) { struct zswap_tree *tree; tree = kzalloc(sizeof(*tree), GFP_KERNEL); if (!tree) { pr_err("alloc failed, zswap disabled for swap type %d\n", type); - return; + return -ENOMEM; } tree->rbroot = RB_ROOT; spin_lock_init(&tree->lock); zswap_trees[type] = tree; + return 0; } void zswap_swapoff(int type)