From patchwork Wed Jan 13 02:42:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Huang, Ying" X-Patchwork-Id: 12015617 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 85F6CC433DB for ; Wed, 13 Jan 2021 02:43:28 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 0280B23122 for ; Wed, 13 Jan 2021 02:43:27 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 0280B23122 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=intel.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id EA0818D0009; Tue, 12 Jan 2021 21:43:26 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E50808D0007; Tue, 12 Jan 2021 21:43:26 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D69588D0009; Tue, 12 Jan 2021 21:43:26 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0232.hostedemail.com [216.40.44.232]) by kanga.kvack.org (Postfix) with ESMTP id C0C578D0007 for ; Tue, 12 Jan 2021 21:43:26 -0500 (EST) Received: from smtpin15.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 8BC393630 for ; Wed, 13 Jan 2021 02:43:26 +0000 (UTC) X-FDA: 77699205612.15.man27_260544d2751a Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin15.hostedemail.com (Postfix) with ESMTP id 685081814B0C1 for ; Wed, 13 Jan 2021 02:43:26 +0000 (UTC) X-HE-Tag: man27_260544d2751a X-Filterd-Recvd-Size: 4848 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by imf17.hostedemail.com (Postfix) with ESMTP for ; Wed, 13 Jan 2021 02:43:24 +0000 (UTC) IronPort-SDR: V+lu+0yW+Ie679tZfzSd+aOb7jPajwqWzh+4tRy3Kq1oU4UhXi2c36NmDWF+REGvM1SoNT6Lhr A9/14mWh0LSA== X-IronPort-AV: E=McAfee;i="6000,8403,9862"; a="157916351" X-IronPort-AV: E=Sophos;i="5.79,343,1602572400"; d="scan'208";a="157916351" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2021 18:43:22 -0800 IronPort-SDR: OO9jggQQWgQP8BJSk/eLKhiVD7g+459JSM+lAK+8wyY/+oqr72Fpra09AJQ2PdnGhHlqauw11M OWywnTOObgng== X-IronPort-AV: E=Sophos;i="5.79,343,1602572400"; d="scan'208";a="353281360" Received: from xizhang-mobl1.ccr.corp.intel.com (HELO yhuang6-mobl1.ccr.corp.intel.com) ([10.254.214.178]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2021 18:43:17 -0800 From: Huang Ying To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Huang Ying , Linus Torvalds , Peter Xu , Hugh Dickins , Johannes Weiner , Mel Gorman , Rik van Riel , Andrea Arcangeli , Michal Hocko , Dave Hansen , Tim Chen Subject: [PATCH] mm: Free unused swap cache page in write protection fault handler Date: Wed, 13 Jan 2021 10:42:41 +0800 Message-Id: <20210113024241.179113-1-ying.huang@intel.com> X-Mailer: git-send-email 2.29.2 MIME-Version: 1.0 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: Commit 09854ba94c6a ("mm: do_wp_page() simplification") introduces an issue as follows. On a system with free memory as follow before test, total used free shared buff/cache available Mem: 1697300 160156 1459220 8648 77924 1419724 Swap: 1048572 0 0 The AnonPages filed of /proc/meminfo is 11712 kB. After running a memory eater which will trigger many swapins and write protection faults, the free memory becomes, total used free shared buff/cache available Mem: 1697300 352620 1309004 624 35676 1252380 Swap: 1048572 216924 831648 While the /proc/meminfo shows, SwapCached: 198908 kB AnonPages: 1956 kB Then, with `swapoff -a`, the free memory becomes, total used free shared buff/cache available Mem: 1697300 161972 1488184 8648 47144 1433172 Swap: 0 0 0 That is, after swapins and write protection faults, many unused swap cache pages will be left unfreed in system. Although the following page reclaiming or swapoff will free these pages, it's still better to free these pages at the first place. So in this patch, at the end of wp_page_copy(), the old unused swap cache page will be tried to be freed. With that, after running the memory eater which will trigger many swapins and write protection faults, the free memory is, total used free shared buff/cache available Mem: 1697300 154020 1509400 1212 33880 1451524 Swap: 1048572 18432 1030140 While the /proc/meminfo shows, SwapCached: 1240 kB AnonPages: 1904 kB BTW: I think this should be in stable after v5.9. Fixes: 09854ba94c6a ("mm: do_wp_page() simplification") Signed-off-by: "Huang, Ying" Cc: Linus Torvalds Cc: Peter Xu Cc: Hugh Dickins Cc: Johannes Weiner Cc: Mel Gorman Cc: Rik van Riel Cc: Andrea Arcangeli Cc: Michal Hocko Cc: Dave Hansen Cc: Tim Chen --- mm/memory.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/mm/memory.c b/mm/memory.c index feff48e1465a..2abaff1befcb 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -2963,6 +2963,11 @@ static vm_fault_t wp_page_copy(struct vm_fault *vmf) munlock_vma_page(old_page); unlock_page(old_page); } + if (page_copied && PageSwapCache(old_page) && + !page_mapped(old_page) && trylock_page(old_page)) { + try_to_free_swap(old_page); + unlock_page(old_page); + } put_page(old_page); } return page_copied ? VM_FAULT_WRITE : 0;