From patchwork Tue Aug 13 09:52:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ge Yang X-Patchwork-Id: 13761677 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 E9B8EC52D7B for ; Tue, 13 Aug 2024 09:54:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 433AC6B00A9; Tue, 13 Aug 2024 05:54:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3E4996B00AA; Tue, 13 Aug 2024 05:54:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2D3676B00AC; Tue, 13 Aug 2024 05:54:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 10B836B00A9 for ; Tue, 13 Aug 2024 05:54:27 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 8A8F9C0745 for ; Tue, 13 Aug 2024 09:54:26 +0000 (UTC) X-FDA: 82446762132.05.53CA493 Received: from m16.mail.126.com (m16.mail.126.com [117.135.210.9]) by imf12.hostedemail.com (Postfix) with ESMTP id 4FE7040019 for ; Tue, 13 Aug 2024 09:54:22 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=126.com header.s=s110527 header.b=NiF98jKQ; spf=pass (imf12.hostedemail.com: domain of yangge1116@126.com designates 117.135.210.9 as permitted sender) smtp.mailfrom=yangge1116@126.com; dmarc=pass (policy=none) header.from=126.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723542794; 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=ceKwBoKd8RVMvMUYur+j2OhO2faLi4b/XH1H8WSU8E0=; b=HZPEP7IsPNCmEJEWvZtAeyfeqQio95OiVwT/pI90B5gGamgkn44x3BsqWD4gh18eFNAv5+ t+gzGR4mBlDc7RCFMuyBBS2MVM3SuAwARdSaGOmBhoHyovfTC6MKeIGRtVRIkAjCp40r5c 2QJNL5rB2sq9mgtko4GjaFXco1vKGbg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723542794; a=rsa-sha256; cv=none; b=mtavoQxtW3shHBYO4R4OZmA8Xi//xy9Sy24r1LoOJWw9MdB0tZvMxCp8OuGnTlR2Dj+x24 zZXSiXxA/kVg8AMDbwnrBqrX3DztDcT6ALHrXshvcxYYFMeSdvQe2sVp2we9BySavubPR2 eeP5X20vmJPokKW2sSymQIHM5rSPoCs= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=126.com header.s=s110527 header.b=NiF98jKQ; spf=pass (imf12.hostedemail.com: domain of yangge1116@126.com designates 117.135.210.9 as permitted sender) smtp.mailfrom=yangge1116@126.com; dmarc=pass (policy=none) header.from=126.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=126.com; s=s110527; h=From:Subject:Date:Message-Id; bh=ceKwBoKd8RVMvMUYur +j2OhO2faLi4b/XH1H8WSU8E0=; b=NiF98jKQMaecPZhVLOEYG/Dy32TH9s8OLi cf+I0LzwwumfUiUHYsx2psycghMKhGppxqAWxYYwe4Vu4JbNLcC3v7JdcBLmq1PW rYXLgEh9MzIFzYHKowXxs+po1qZSXWtYBLhjIykbpOBBEk7L1dcO6TEySsoum56/ wRSV5rpVQ= Received: from hg-OptiPlex-7040.hygon.cn (unknown [118.242.3.34]) by gzga-smtp-mta-g2-4 (Coremail) with SMTP id _____wDnT3XaLLtmUKKJAQ--.43172S2; Tue, 13 Aug 2024 17:52:28 +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, yuzhao@google.com, liuzixing@hygon.cn, yangge Subject: [PATCH V3] mm/swap: take folio refcount after testing the LRU flag Date: Tue, 13 Aug 2024 17:52:23 +0800 Message-Id: <1723542743-32179-1-git-send-email-yangge1116@126.com> X-Mailer: git-send-email 2.7.4 X-CM-TRANSID: _____wDnT3XaLLtmUKKJAQ--.43172S2 X-Coremail-Antispam: 1Uf129KBjvJXoW7Xw4UArWfWw4rWw47uw4DJwb_yoW8Jry8pa 18GwnIgrZ3Gw1jva47Zw13AryYkayI9F4UAayfCwnruF15JwnFkF1fKw4UZay5CryxWF48 uFy5tF1kZ3ZFvFDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0zR58n5UUUUU= X-Originating-IP: [118.242.3.34] X-CM-SenderInfo: 51dqwwjhrrila6rslhhfrp/1tbiOhM5G2VEyJ36hAADsC X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 4FE7040019 X-Stat-Signature: 98rytmpxkpxk7zquak8jbbed997kdk38 X-HE-Tag: 1723542862-44081 X-HE-Meta: U2FsdGVkX1/Ur4ccxMZlqsUGwwbnEhPzrikm4XznM5hmeZVJ/sezktyOnApFyRBt4rJXajYs4qSDtqL1o6/KjI35CcNLCHGe8OznisSgqA+go4vACbfhliiSTXAdn+Z8RpPmX3OTJ0U5rcN1iEonhJ2aItTXdGh3KYbM8NYhfPvCz8hyMrImSenCMSWXfkn0Rzl1q4o49tXJMj0G1TKPEllEoA0r9Ri0zXGYILZbB3hf0Q/0j1STriotptK/y04nHI8Tw2o+uFMolbIm7Oy5HmcGGfFykQfApXFKRd3dWUnSVXu3YMR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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 | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) V3: Remove sanity check suggested by Yu V2: Add sanity check suggested by David diff --git a/mm/swap.c b/mm/swap.c index 67a2467..6b83898 100644 --- a/mm/swap.c +++ b/mm/swap.c @@ -226,12 +226,10 @@ 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; - } + + folio_get(folio); if (disable_irq) local_lock_irqsave(&cpu_fbatches.lock_irq, flags);