From nobody Sat May 4 00:48:04 2024 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 86AF6C4708D for ; Mon, 5 Dec 2022 02:47:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231226AbiLECrv (ORCPT ); Sun, 4 Dec 2022 21:47:51 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46592 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230307AbiLECrs (ORCPT ); Sun, 4 Dec 2022 21:47:48 -0500 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0281365D0 for ; Sun, 4 Dec 2022 18:47:47 -0800 (PST) Received: from dggpemm500002.china.huawei.com (unknown [172.30.72.53]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4NQScd5mJCzRpkX; Mon, 5 Dec 2022 10:46:57 +0800 (CST) Received: from localhost.localdomain (10.175.112.125) by dggpemm500002.china.huawei.com (7.185.36.229) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Mon, 5 Dec 2022 10:47:44 +0800 From: Chen Wandun To: , , , , , , CC: Subject: [PATCH] swapfile: fix uncorrect statistical count about free swap Date: Mon, 5 Dec 2022 10:47:26 +0800 Message-ID: <20221205024726.3111808-1-chenwandun@huawei.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.175.112.125] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To dggpemm500002.china.huawei.com (7.185.36.229) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The counter of free swap turn to negtive during swapoff, this phenomenon can be observed in oom log, such as: 1226 total pagecache pages 47 pages in swap cache Swap cache stats: add 119633, delete 119814, find 26832/82125 Free swap =3D -31796kB Total swap =3D 0kB 1048576 pages RAM Subtract the size of swap partition from free swap should put behind try_to_unuse, otherwise, it will result in negtive counter if swap partition is in use. Fixes: ec8acf20afb8 ("swap: add per-partition lock for swapfile") Signed-off-by: Chen Wandun --- mm/swapfile.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 72e481aacd5d..5b9b695be84b 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -2316,8 +2316,6 @@ static void setup_swap_info(struct swap_info_struct *= p, int prio, static void _enable_swap_info(struct swap_info_struct *p) { p->flags |=3D SWP_WRITEOK; - atomic_long_add(p->pages, &nr_swap_pages); - total_swap_pages +=3D p->pages; =20 assert_spin_locked(&swap_lock); /* @@ -2353,6 +2351,8 @@ static void enable_swap_info(struct swap_info_struct = *p, int prio, spin_lock(&swap_lock); spin_lock(&p->lock); _enable_swap_info(p); + atomic_long_add(p->pages, &nr_swap_pages); + total_swap_pages +=3D p->pages; spin_unlock(&p->lock); spin_unlock(&swap_lock); } @@ -2444,8 +2444,6 @@ SYSCALL_DEFINE1(swapoff, const char __user *, special= file) least_priority++; } plist_del(&p->list, &swap_active_head); - atomic_long_sub(p->pages, &nr_swap_pages); - total_swap_pages -=3D p->pages; p->flags &=3D ~SWP_WRITEOK; spin_unlock(&p->lock); spin_unlock(&swap_lock); @@ -2500,6 +2498,8 @@ SYSCALL_DEFINE1(swapoff, const char __user *, special= file) spin_lock(&p->lock); } =20 + atomic_long_sub(p->pages, &nr_swap_pages); + total_swap_pages -=3D p->pages; swap_file =3D p->swap_file; old_block_size =3D p->old_block_size; p->swap_file =3D NULL; --=20 2.25.1