From patchwork Mon Dec 5 02:47:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chen Wandun X-Patchwork-Id: 13064096 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 0211CC4321E for ; Mon, 5 Dec 2022 02:47:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 718778E0002; Sun, 4 Dec 2022 21:47:51 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6C9448E0001; Sun, 4 Dec 2022 21:47:51 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 590988E0002; Sun, 4 Dec 2022 21:47:51 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 462438E0001 for ; Sun, 4 Dec 2022 21:47:51 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 1C3501A03A8 for ; Mon, 5 Dec 2022 02:47:51 +0000 (UTC) X-FDA: 80206717542.15.45F5BA4 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by imf06.hostedemail.com (Postfix) with ESMTP id 9EBE1180009 for ; Mon, 5 Dec 2022 02:47:49 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf06.hostedemail.com: domain of chenwandun@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=chenwandun@huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1670208470; 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-type: content-transfer-encoding:content-transfer-encoding:in-reply-to: references; bh=JZeT/YesKFGIbQIj1gzkV5xN/Eo3Dre27Vfb3WyYh0Q=; b=kKAzGC3K+TotalZoyvXCp96N395yypdf/SAL/xxAcxbMUv941AjSSXKmbrsNF25cYwRNpC uX1bxqjxXpa3yQAcIR3KEOO51GTtbxeqHCjnnSnvvr4JJ1goU2wmjI3CxT1EXzoSpUBADL x/WA0ZhYGb8t/t7PLSRG+HpoSShg0Ls= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf06.hostedemail.com: domain of chenwandun@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=chenwandun@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1670208470; a=rsa-sha256; cv=none; b=y6FPEC/L3z1sW3PkrlKXqGcijXogmlBSwV1d6JReyP5ovhZ58R6UIWjlGIboo4gcg+fI/N fw0gHvHBInZyTZK8KUSr2aCKW+TeA5QUTw9YOzAK5Y1DWeIHRjj0E2x+JuvCVALA/tclDv lJQEzFS3hTOaLK92Q0hL5KPLC5Lztjk= 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 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 X-Spamd-Result: default: False [-0.52 / 9.00]; BAYES_HAM(-3.32)[92.88%]; R_MISSING_CHARSET(2.50)[]; MID_CONTAINS_FROM(1.00)[]; DMARC_POLICY_ALLOW(-0.50)[huawei.com,quarantine]; R_SPF_ALLOW(-0.20)[+ip4:45.249.212.187/29]; MIME_GOOD(-0.10)[text/plain]; RCVD_NO_TLS_LAST(0.10)[]; R_DKIM_NA(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; RCPT_COUNT_SEVEN(0.00)[8]; RCVD_COUNT_THREE(0.00)[3]; MIME_TRACE(0.00)[0:+]; HAS_XOIP(0.00)[]; TO_MATCH_ENVRCPT_SOME(0.00)[]; FROM_HAS_DN(0.00)[]; ARC_SIGNED(0.00)[hostedemail.com:s=arc-20220608:i=1]; TO_DN_NONE(0.00)[]; ARC_NA(0.00)[] X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 9EBE1180009 X-Stat-Signature: i73t3wy3nh1nobhp3nzdesu7ere617rz X-HE-Tag: 1670208469-852331 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: 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 = -31796kB Total swap = 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 |= SWP_WRITEOK; - atomic_long_add(p->pages, &nr_swap_pages); - total_swap_pages += p->pages; 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 += p->pages; spin_unlock(&p->lock); spin_unlock(&swap_lock); } @@ -2444,8 +2444,6 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) least_priority++; } plist_del(&p->list, &swap_active_head); - atomic_long_sub(p->pages, &nr_swap_pages); - total_swap_pages -= p->pages; p->flags &= ~SWP_WRITEOK; spin_unlock(&p->lock); spin_unlock(&swap_lock); @@ -2500,6 +2498,8 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) spin_lock(&p->lock); } + atomic_long_sub(p->pages, &nr_swap_pages); + total_swap_pages -= p->pages; swap_file = p->swap_file; old_block_size = p->old_block_size; p->swap_file = NULL;