From patchwork Mon Nov 13 13:06:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhongkun He X-Patchwork-Id: 13453971 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 BE09FC4167D for ; Mon, 13 Nov 2023 13:06:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4C6966B01F0; Mon, 13 Nov 2023 08:06:21 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 476936B01F4; Mon, 13 Nov 2023 08:06:21 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 33EA56B01F6; Mon, 13 Nov 2023 08:06:21 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 22F5A6B01F0 for ; Mon, 13 Nov 2023 08:06:21 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 0095BA07D2 for ; Mon, 13 Nov 2023 13:06:20 +0000 (UTC) X-FDA: 81452954520.07.9B6EDC7 Received: from mail-pf1-f177.google.com (mail-pf1-f177.google.com [209.85.210.177]) by imf16.hostedemail.com (Postfix) with ESMTP id 7799018002B for ; Mon, 13 Nov 2023 13:06:17 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=gpD+Pzst; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf16.hostedemail.com: domain of hezhongkun.hzk@bytedance.com designates 209.85.210.177 as permitted sender) smtp.mailfrom=hezhongkun.hzk@bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1699880779; 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-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=iXoWjqfg8+j8eT28RHElwz66GqwL0VNwqWlpAIhmXQc=; b=RbAxMvLfcOwfjzKDo32AlBJg/I4aZOdjgc5Ew0uH7cVtYHPIr63ofyYez3GgWexriHJr7f TBi8b6nXlhid3vBZe4HBgAPPoYagMG8dn2T9Rv6RM0q5v6cykcpUHgtQQgyJ2ZpVsXJNbM gEKnIxbX05JCPuK2KSkV7G6tG2g2OF8= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=gpD+Pzst; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf16.hostedemail.com: domain of hezhongkun.hzk@bytedance.com designates 209.85.210.177 as permitted sender) smtp.mailfrom=hezhongkun.hzk@bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1699880779; a=rsa-sha256; cv=none; b=3opkQpBcPSJJ47yUS/VY1hCUAzZ2iGBXnJ9eY2WTZiHwXnD04x9WhBcoW/vHuf5jOBg4Tz Xw5ePExA+9k4OunEIDh6YHnk+4k/jcbbrkv5CMTPoLViISYdAyNvoA1OHpDFTaxqrkMisN VHzmXJQ3MxnfKXcQZ24X3wkuC+lnJ6Y= Received: by mail-pf1-f177.google.com with SMTP id d2e1a72fcca58-6b7f0170d7bso4370249b3a.2 for ; Mon, 13 Nov 2023 05:06:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1699880776; x=1700485576; darn=kvack.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=iXoWjqfg8+j8eT28RHElwz66GqwL0VNwqWlpAIhmXQc=; b=gpD+PzstGDUTfUXgyETnX6ZkhrY50/3mRz9lSwYWNeEJa2Wz4+RnzYT+3Xto0+PyMW lHU5cRcd5KHiry4s8x1vQidfpPnK180X9jRy/SAaIMoGcHdSshHrKXxDte+4hTt628YO CyT0m84cZxwSA/ZrgQFYhJ6qcXpTRb6yk0rZ2Bqxtj0mmqGNhoeI2U3H6aGNkicTNtzY jbkFF+S6+ZDw1Z2a4IXq8fWVQ1pXsxNgkEeEVa/G0ZBDVII6hGR1XYNfiwOoFgVUhjA4 n0HQUqexRH2+KKBdXxHrsmkFZt4RG4rMqVBLll0iOADU5otnrojRAtFsNS69z5NRg82P 7BXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699880776; x=1700485576; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=iXoWjqfg8+j8eT28RHElwz66GqwL0VNwqWlpAIhmXQc=; b=I8oVZXXtdrOuf+FP3mUgTOhrXWmJex/KmspE1BiWJbTPt7lxjD9Tb8qHVV2Zcd2ZGg SQwyUQv0jlmdmq+Y7Iee5a728Sjc590ShWtUzid5CSNVG0BFxqVqjl6pLcmeqxvfM+vM 9UKK9Krz/JY29uvbCmvdWZonOqIcV0xI23pC9/tE/qg2/CIiiu2ItxAOyrOkW0AoYTWJ JsWfBk2sqWFRs0UqPvkXTEHfQu05g9zIPqy/WnTZlKIpp02ikXwzRYB5UXHV/U5CWY9W 7C7F6wuOKsXoV3w4qiCW+ME0AJimnG4H0WjagBwuPs2WCWp/X56G/wk/EwcF9VTvbXzJ H66g== X-Gm-Message-State: AOJu0YxnnRY5NzjxESlDyX0V4qtch4OZrZjFVPhE0tjnUdzMmJDGCFsd 6a+QBxyp06H7cxW21++0ewnCtg== X-Google-Smtp-Source: AGHT+IHJ3nZBgscbZNQTk6JtG1TVoFaqi3hf5LL5vRLXl4Ospw7faDez8f3mP1+E9odJmLya4BzKgA== X-Received: by 2002:a05:6a00:1406:b0:6bc:e7f8:821e with SMTP id l6-20020a056a00140600b006bce7f8821emr8344990pfu.10.1699880775952; Mon, 13 Nov 2023 05:06:15 -0800 (PST) Received: from localhost.localdomain ([203.208.167.146]) by smtp.gmail.com with ESMTPSA id g3-20020aa78743000000b0068ff0a633fdsm3810805pfo.131.2023.11.13.05.06.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Nov 2023 05:06:15 -0800 (PST) From: Zhongkun He 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 Subject: [PATCH] mm:zswap: fix zswap entry reclamation failure in two scenarios Date: Mon, 13 Nov 2023 21:06:01 +0800 Message-Id: <20231113130601.3350915-1-hezhongkun.hzk@bytedance.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 7799018002B X-Stat-Signature: qa3jyz3yendwz8wrd5d48rn31riufdcj X-HE-Tag: 1699880777-33975 X-HE-Meta: U2FsdGVkX18haxVxdYCulm7AsNj4UXovTjkEGD8njGY+2ZIiStPNh+ou3sP3A1OBYD4I6r/I+0H2vUzs+Je0o/FenIsLWUNweOVGQdd42/A7qkbRuCc4KQYmIwhrFobEnmraEMZLQSeSYEzMcpy5JL84QNF0ouhvgSxjFdio7/YeIO0+ldpGh9Qndt19RvmSeI+ciDVGVZi5flXkOgkaOjSQ8hWCWI6kE5Cqv3jfFMj9QXSjv0nX4YfGtjF/pVX71Fb7Tgszso6dDokiXpiwNn0qvE/R4FPOsWTbXHFYR6ySK/Ofl4D9kv8NLf7tYPJXqIfyJDderUsk8B8Wl42ugHK5q0WSr/oCRhVupMKwLp6jHOSsKB/8FKASCygBbPFCBeHdihIC3wbdItKkTl0AHoz0FBPSQXPDveTL50v+OoyAnvY5FQcJVlaQI3s4uhcepailOm8yn1Xrw6ZHcWCbPNirUTkf68c5Nx0Bruc/orDAYVgwGpySWlVfnwWufL158ZcxbRmzBBXfH3x+xoUPU7jQYsZy6TTwkjPKxTIvAkKJWUsB/QDn7Kg6vruCd3O/36JnqxCIt30BIMrhnnlDn+oWTIxyU5mWvHPYbD+sUw59Nt3wOPTpWtVZ84wBH3X7mFgeyCSKcT66e0YA0ZErU6LwfXsddx+O/qD0FXyT58qhPXaYFAaLf6YUfMaimA2tPxpS4+XxMa1a0DplURdy6zcgOHGUUyMXsC5rbc+RYLHFOsnF6O0VDzvSV4id3lAxPl2hbnYK7JXRApBuFLpGnNeqVO5dIGo5Mz/VMMSRKeSZRipjtEjjUjLOhMBuCLnvJHgQPT//TS9OObWcb5r+vD3MPFKxEdWF8tuZX62ziaRTl/YddUn4M2tRj6bs03f+6pOwyGgqt5WTMgm66HyFmhSVze7QiVianX5yWbAQXjXQTcXNkze4W25dWdWy+4fXPbL+WvO9mItO8/mWgi4 IF383UPc DWwNKWFuVYCNIy0PWTag1v+qr1IgJ0hoXAKkWWCDM07PoLuyooLnf/HHdru+wEFTs/RwGycDCYlQKcMdnYBoEqa1NjfVQUICexG1wkmVwz/3GU1Jfe+L5PDQF8pJQDN7jkBt2n8RaNwIumDQXlsauNQ4WSkhWH22JVJVGw9HJfXI+6No18SWttcKw1Rh/lzoGY8PVGEiiRYKnXCi9BmvRUl+W9cb3hd2bOlvg8GPKeLID7Crpr/P5/YgcRjOnzffsQo3nzK0dHyMijRzc/aEOgQS/3UX+ab99uU56KV2kS8BdzP2aHEyQsX3ue6ROLV8aHwSHeu5tlOQHOF0zrJ42PlS5HMN5FZxmNAWc3OtHLliUv1Uq+GNBIC83CYQH2E3D6ENFtbeehhMatlfHDqtIyJ6qrmzs5c+vx0/R 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: 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 Reviewed-by: Nhat Pham --- 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);