From patchwork Mon Aug 12 11:21:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ge Yang X-Patchwork-Id: 13760443 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 7259AC3DA7F for ; Mon, 12 Aug 2024 11:22:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E024B6B0092; Mon, 12 Aug 2024 07:22:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DB2056B0098; Mon, 12 Aug 2024 07:22:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CA04E6B009A; Mon, 12 Aug 2024 07:22:14 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id ACDDF6B0092 for ; Mon, 12 Aug 2024 07:22:14 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 65536160A53 for ; Mon, 12 Aug 2024 11:22:14 +0000 (UTC) X-FDA: 82443354588.24.76E564F Received: from m16.mail.126.com (m16.mail.126.com [220.197.31.8]) by imf04.hostedemail.com (Postfix) with ESMTP id A5FFA40011 for ; Mon, 12 Aug 2024 11:22:11 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=126.com header.s=s110527 header.b=hBt59s0Q; dmarc=pass (policy=none) header.from=126.com; spf=pass (imf04.hostedemail.com: domain of yangge1116@126.com designates 220.197.31.8 as permitted sender) smtp.mailfrom=yangge1116@126.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723461655; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:references:dkim-signature; bh=/KCDaGayguIyv7wyA6Oo5OBqacUsg/QIQhHO9iUM0eg=; b=eLnPIpRlQGX8qgmjBoGeTyK90Dlt4+ed4Ohj0jhi9rh+bTeFEdfX8cf5DBg7KIFRW1A4/V kMbhfqkcFkVViM0lSZXhcg2rifyOUzD2W/WtY09hbHyz3dG9oAjHbrg2aZUmHgEPE9yFWQ nJ/th1yNCmQ7PW/Q3+QAYN+3rXRaRo0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723461655; a=rsa-sha256; cv=none; b=xw2TKBXiL5g5Pu1g8mO3tEf3dGQSP/4lq50S1n1XpxBnczk26L89GDswlpvc5FY3dQHb2w FYVLFMs/u98lEjfMKcFPTXk0JgEKih2ItPEKl/qDNktXnzuUvnci/vgDAvHYApSWpsr66u nv4GqfjJ156Eg/S1E/p9TEPVRma8wQc= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=126.com header.s=s110527 header.b=hBt59s0Q; dmarc=pass (policy=none) header.from=126.com; spf=pass (imf04.hostedemail.com: domain of yangge1116@126.com designates 220.197.31.8 as permitted sender) smtp.mailfrom=yangge1116@126.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=126.com; s=s110527; h=From:Subject:Date:Message-Id; bh=/KCDaGayguIyv7wyA6 Oo5OBqacUsg/QIQhHO9iUM0eg=; b=hBt59s0QQNCyGROplKSQSIbNNY2EjvKuaH DENQoX1DAx9Q0SrXGSiu5t9dA1+sJYl/8Xs27e/zQt8pgQ2Hi/G78AN29IUc6wOJ XosZKsJfYK6UvUEF7Id3nzj+i2m9zBvaxCGkvTc1Oi8YcwibvQhIoanlLF0l5apB H1UlGb/KU= Received: from hg-OptiPlex-7040.hygon.cn (unknown [118.242.3.34]) by gzga-smtp-mta-g2-0 (Coremail) with SMTP id _____wDnF3pY8LlmoNaoAQ--.25519S2; Mon, 12 Aug 2024 19:22:02 +0800 (CST) From: yangge1116@126.com To: akpm@linux-foundation.org Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, 21cnbao@gmail.com, david@redhat.com, baolin.wang@linux.alibaba.com, liuzixing@hygon.cn, yangge Subject: [PATCH V2] mm/swap: take folio refcount after testing the LRU flag Date: Mon, 12 Aug 2024 19:21:58 +0800 Message-Id: <1723461718-5503-1-git-send-email-yangge1116@126.com> X-Mailer: git-send-email 2.7.4 X-CM-TRANSID: _____wDnF3pY8LlmoNaoAQ--.25519S2 X-Coremail-Antispam: 1Uf129KBjvJXoW7Xw4UArWfWw4rWw47uw4DJwb_yoW8JrW8pw 48GwnIgrZ7Gw1jy3W7Zwn8A34jkFWI9r48AFWfCr17uF15J3ZFkF1xtw4jvay5uryxWF18 ZFZ5tF1kXF1jvFDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0zRkhLnUUUUU= X-Originating-IP: [118.242.3.34] X-CM-SenderInfo: 51dqwwjhrrila6rslhhfrp/1tbiOhQ5G2VEyJkENQABsM X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: A5FFA40011 X-Stat-Signature: u1sdiuwmt46yskyw3og8ee1a8wxwfn4j X-Rspam-User: X-HE-Tag: 1723461731-921208 X-HE-Meta: U2FsdGVkX1/YKwAMAumefc602bnGBML7jih4YZb2+f1qg3+uvMJOkvncvklo0BNKGhiA6QnsKAw/TtbVyIuS/CORdY07P9mvhAgyxTvRf59gAqiS2aQ0Bb+agiJKcqgU8pT7aW7+qKxYhY8FDNgL7AAyYhdv+UxrgZvcYa36FEODhsZD2QlvHuKif9dHyzyqZ7JTy5+vJcUncg11LKL4VkjGeo9H6d1VIfUVPxDkjz6RLde357PqXl7rtxlqBitxPC4kNiaXNKySVC0xsGg90aysbkVn7Jb/v6plGiPiILzeHOkMWxI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iX1XP88RXwA6qPAW7hbiDq2s9tQcpJBEmWU9dsg= 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: From: yangge Whoever passes a folio to __folio_batch_add_and_move() must hold a reference, otherwise something else would already be messed up. If the folio is referenced, it will not be freed elsewhere, so we can safely clear the folio's lru flag. As discussed with David in [1], we should take the reference after testing the LRU flag, not before. Link: https://lore.kernel.org/lkml/d41865b4-d6fa-49ba-890a-921eefad27dd@redhat.com/ [1] Signed-off-by: yangge Acked-by: David Hildenbrand --- mm/swap.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) V2: Add sanity check suggested by David diff --git a/mm/swap.c b/mm/swap.c index 67a2467..c048659 100644 --- a/mm/swap.c +++ b/mm/swap.c @@ -226,12 +226,11 @@ static void __folio_batch_add_and_move(struct folio_batch __percpu *fbatch, { unsigned long flags; - folio_get(folio); - - if (on_lru && !folio_test_clear_lru(folio)) { - folio_put(folio); + if (on_lru && !folio_test_clear_lru(folio)) return; - } + + VM_WARN_ON_ONCE(!folio_ref_count(folio)); + folio_get(folio); if (disable_irq) local_lock_irqsave(&cpu_fbatches.lock_irq, flags);