All of lore.kernel.org
 help / color / mirror / Atom feed
From: Zhongkun He <hezhongkun.hzk@bytedance.com>
To: akpm@linux-foundation.org
Cc: hannes@cmpxchg.org, yosryahmed@google.com, nphamcs@gmail.com,
	sjenning@redhat.com, ddstreet@ieee.org, vitaly.wool@konsulko.com,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	Zhongkun He <hezhongkun.hzk@bytedance.com>
Subject: [PATCH] mm:zswap: fix zswap entry reclamation failure in two scenarios
Date: Mon, 13 Nov 2023 21:06:01 +0800	[thread overview]
Message-ID: <20231113130601.3350915-1-hezhongkun.hzk@bytedance.com> (raw)

I recently found two scenarios where zswap entry could not be
released, which will cause shrink_worker and active recycling
to fail.
1)The swap entry has been freed, but cached in swap_slots_cache,
no swap cache and swapcount=0.
2)When the option zswap_exclusive_loads_enabled disabled and
zswap_load completed(page in swap_cache and swapcount = 0).

The above two cases need to be determined by swapcount=0,
fix it.

Signed-off-by: Zhongkun He <hezhongkun.hzk@bytedance.com>
---
 mm/zswap.c | 35 +++++++++++++++++++++++++----------
 1 file changed, 25 insertions(+), 10 deletions(-)

diff --git a/mm/zswap.c b/mm/zswap.c
index 74411dfdad92..db95491bcdd5 100644
--- a/mm/zswap.c
+++ b/mm/zswap.c
@@ -1063,11 +1063,12 @@ static int zswap_writeback_entry(struct zswap_entry *entry,
 	struct mempolicy *mpol;
 	struct scatterlist input, output;
 	struct crypto_acomp_ctx *acomp_ctx;
+	struct swap_info_struct *si;
 	struct zpool *pool = zswap_find_zpool(entry);
 	bool page_was_allocated;
 	u8 *src, *tmp = NULL;
 	unsigned int dlen;
-	int ret;
+	int ret = 0;
 	struct writeback_control wbc = {
 		.sync_mode = WB_SYNC_NONE,
 	};
@@ -1082,16 +1083,30 @@ static int zswap_writeback_entry(struct zswap_entry *entry,
 	mpol = get_task_policy(current);
 	page = __read_swap_cache_async(swpentry, GFP_KERNEL, mpol,
 				NO_INTERLEAVE_INDEX, &page_was_allocated);
-	if (!page) {
+	if (!page)
 		ret = -ENOMEM;
-		goto fail;
-	}
-
-	/* Found an existing page, we raced with load/swapin */
-	if (!page_was_allocated) {
+	else if (!page_was_allocated) {
+		/* Found an existing page, we raced with load/swapin */
 		put_page(page);
 		ret = -EEXIST;
-		goto fail;
+	}
+
+	if (ret) {
+		si = get_swap_device(swpentry);
+		if (!si)
+			goto out;
+
+		/* Two cases to directly release zswap_entry.
+		 * 1) -ENOMEM,if the swpentry has been freed, but cached in
+		 * swap_slots_cache(no page and swapcount = 0).
+		 * 2) -EEXIST, option zswap_exclusive_loads_enabled disabled and
+		 * zswap_load completed(page in swap_cache and swapcount = 0).
+		 */
+		if (!swap_swapcount(si, swpentry))
+			ret = 0;
+
+		put_swap_device(si);
+		goto out;
 	}
 
 	/*
@@ -1106,7 +1121,7 @@ static int zswap_writeback_entry(struct zswap_entry *entry,
 		spin_unlock(&tree->lock);
 		delete_from_swap_cache(page_folio(page));
 		ret = -ENOMEM;
-		goto fail;
+		goto out;
 	}
 	spin_unlock(&tree->lock);
 
@@ -1151,7 +1166,7 @@ static int zswap_writeback_entry(struct zswap_entry *entry,
 
 	return ret;
 
-fail:
+out:
 	if (!zpool_can_sleep_mapped(pool))
 		kfree(tmp);
 
-- 
2.25.1


             reply	other threads:[~2023-11-13 13:06 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-13 13:06 Zhongkun He [this message]
2023-11-13 15:11 ` [PATCH] mm:zswap: fix zswap entry reclamation failure in two scenarios Nhat Pham
2023-11-14  5:21   ` [External] " 贺中坤
2023-11-14 16:30     ` Nhat Pham
2023-11-15 12:12       ` 贺中坤
2023-11-14 17:16 ` Yosry Ahmed
2023-11-15 12:53   ` [External] " 贺中坤
2023-11-15 20:12     ` Yosry Ahmed
2023-11-16  3:33       ` 贺中坤
2023-11-16  4:09         ` Yosry Ahmed
2023-11-16  4:23           ` 贺中坤
2023-11-16  8:31   ` Huang, Ying
2023-11-16 10:34     ` [External] " 贺中坤
2023-11-16 20:11   ` Chris Li
2023-11-16 20:18     ` Yosry Ahmed
2023-11-16 20:30       ` Chris Li
2023-11-16 20:45         ` Yosry Ahmed
2023-11-17 23:30           ` Chris Li
2023-11-17  9:56         ` [External] " Zhongkun He
2023-11-17 23:47           ` Chris Li
2023-11-18  1:45             ` Zhongkun He
2023-11-18 18:43               ` Nhat Pham
2023-11-19  8:29                 ` Chris Li
2023-11-20  2:42                 ` Zhongkun He
2023-11-19  8:23               ` Chris Li
2023-11-20  3:16                 ` Zhongkun He
2023-11-20  3:18         ` Huang, Ying
2023-11-20  5:31           ` Chris Li
2023-11-20  5:39             ` Huang, Ying
2023-11-20  5:51               ` Chris Li
2023-11-20 18:52           ` Yosry Ahmed
2023-11-21  0:54             ` Huang, Ying
2023-11-21  1:15               ` Yosry Ahmed
2023-11-21  1:53                 ` Huang, Ying
2023-11-21  2:46                   ` Yosry Ahmed
2023-11-21  3:32                     ` Huang, Ying
2023-11-21  3:37                       ` Yosry Ahmed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231113130601.3350915-1-hezhongkun.hzk@bytedance.com \
    --to=hezhongkun.hzk@bytedance.com \
    --cc=akpm@linux-foundation.org \
    --cc=ddstreet@ieee.org \
    --cc=hannes@cmpxchg.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=nphamcs@gmail.com \
    --cc=sjenning@redhat.com \
    --cc=vitaly.wool@konsulko.com \
    --cc=yosryahmed@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.