From patchwork Tue Aug 29 02:41:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liu Shixin X-Patchwork-Id: 13368504 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 D4815C83F12 for ; Tue, 29 Aug 2023 01:46:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E3D0B28002D; Mon, 28 Aug 2023 21:46:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DED168E001E; Mon, 28 Aug 2023 21:46:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CB4CE28002D; Mon, 28 Aug 2023 21:46:55 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id BB22E8E001E for ; Mon, 28 Aug 2023 21:46:55 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 88E93C0731 for ; Tue, 29 Aug 2023 01:46:55 +0000 (UTC) X-FDA: 81175453590.15.036562D Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by imf13.hostedemail.com (Postfix) with ESMTP id 0A4DE20009 for ; Tue, 29 Aug 2023 01:46:51 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=none; spf=pass (imf13.hostedemail.com: domain of liushixin2@huawei.com designates 45.249.212.189 as permitted sender) smtp.mailfrom=liushixin2@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1693273613; 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=OoM6nH4OzWV51z2dIrgaBbCQgC29HjrNZMTs5eWv3Og=; b=ms5geKDnwZeuazR702l24FyWoz7uvqd517W2fTVO4w9ArkB430vH2fFPZbG8AxyPOt425j BFODPzvpj6MiFa+iOVC7XOqOLFrEeqDNPDmSo6OgCIPmHNICy6uHk5eOg/WjpPVq/5b4oc 2Dijvml8MOMdP+Sf3qyEBBJSBTF02gI= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1693273613; a=rsa-sha256; cv=none; b=oXNzbKcckggryWUqujlEiBX4phinYd0ask9wOGAPr8+g9nnzjgyKYyZti3sQFiMcfWR9Fz durVf6MxDlNAu+y82nBzu2urtXDDtp0oTArM0NN0PQmii1wWAAs+kDj3r0Oyrf6JvGYElx jfc/FJ1DVhRMtoEQQszJHtLuyFN9wSw= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=none; spf=pass (imf13.hostedemail.com: domain of liushixin2@huawei.com designates 45.249.212.189 as permitted sender) smtp.mailfrom=liushixin2@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com Received: from dggpemm500009.china.huawei.com (unknown [172.30.72.53]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4RZVZJ41KtzLpF1; Tue, 29 Aug 2023 09:43:36 +0800 (CST) Received: from huawei.com (10.175.113.32) by dggpemm500009.china.huawei.com (7.185.36.225) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.31; Tue, 29 Aug 2023 09:46:47 +0800 From: Liu Shixin To: Yosry Ahmed , Huang Ying , Michal Hocko , Johannes Weiner , Roman Gushchin , Shakeel Butt , Muchun Song , Andrew Morton , CC: , , , Liu Shixin Subject: [PATCH v4] mm: vmscan: try to reclaim swapcache pages if no swap space Date: Tue, 29 Aug 2023 10:41:04 +0800 Message-ID: <20230829024104.1505530-1-liushixin2@huawei.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Originating-IP: [10.175.113.32] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To dggpemm500009.china.huawei.com (7.185.36.225) X-CFilter-Loop: Reflected X-Stat-Signature: akxkgypdir1zxo37tfk17k19r57yajz5 X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 0A4DE20009 X-Rspam-User: X-HE-Tag: 1693273611-699526 X-HE-Meta: U2FsdGVkX1/3vf1nZlPSy4e6qxEfa1mnBLlwFDU8VpSoCMlR50kQXKvTjpN/3WV1m35pg0jwYEi5lBrqEIwlOlDwfoOaV18WSGhXapcYA0ypmIv2A7Gs8Koj4Oj8zP8MPka0yPsZ26TetuQAxXtVYPn7l1yOeGrvw3WiCBsmb6p0hrmQg2XgpIHWlFHvwI+6R1lNdoYQ2n0yCwuX0qphTdROZYmUrRNVdOoyg+aialsClr0RY0pjdm6kCNyYFDBzs+G9gHO4dcSVtg7HpRiFBi1pxAwMn/DF8FlhvsPYG5mk29fMdl4UCG6EgrhXUVQ2xphlHzS/o/NozFmRANu+vmpTVAwv2F6QIcHLebNyb6inXHTxmG/6WZAMytrco1AG68d/LJv6dcuIMUcc/aSJ0iKZduyr8xruCUsYNgYoMAnncaNQq7iXBhVMMRjC1BthM836A4CjzwlXHTGz9rFPJu5oRwFTe0dMsteyo0Saeu1g3ro2DrAeCKPCI5VsNgmhHLO/8+blPrNvcL3xrtmQ5F3YosqPP0+SSarnHcKLL7Rmf6g4sgNrBhH+oj1B+/JJFURN1yzPR/0Ebm6JIeyuOfjJY/7ZDCA3RkmiaN1DzKbR63GXtpmFye57SUWGnLp84AMg+Hb/Mr7Lxb+3eE2GaEqZrA+nPkkw6NE7dNdkUEN6LUqcH50wZK4dXcvl8bvpKtSrTVhAFmaDeotq6zLHLIzWlrsmgoJarl4cosnY8eYFpvTogyvYf9sEvuzV/ZNgnBfdEujGNc1qqNFLRuhleHKYX9W85TwmlHAzfwrE3UVSvIl19PgMpG8xYoSq655J5RSGNhbyeiTIoFVUkl86pHVAgCyQEEP6Ics3U7VVsYczsL/+vhrTGJMPjvQcgiXnOVSDFzza6FEhc+TWZ8bLqg8gyQdlqgwW4eq+OxkIqKWinLp/b/YNGD/qa3VYr4lCar90OGbosIePrEzCiMY 7B35Ep8f uuyUTGwd+tzedg7fZ5oVDytF5IdeIwOuxJd4BWBPLFormIFGGtSiRBfTOVlo4qujsQR9uDqD13t1u3np/UWRpm5AVBXmckg0iG4ywYdSiv6g/uontcWZi/kusQ+MojCqg6y2DISlcsW+p60LVBS3fAh9k0k27/NLMizxbW3M05gWbP0o6GJBiakEQiZlvKa20kkP119EQ7hm8NlpU4Yn7z2cfHAvRx7GrVjF4I9O3/+0RvbHVdfzRgszo+qcGuhv6WSmAKi3Q3bNuy24CtT7S0JigdQ== 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: When spaces of swap devices are exhausted, only file pages can be reclaimed. But there are still some swapcache pages in anon lru list. This can lead to a premature out-of-memory. The problem is found with such step: Firstly, set a 9MB disk swap space, then create a cgroup with 10MB memory limit, then runs an program to allocates about 15MB memory. The problem occurs occasionally, which may need about 100 times. Fix it by checking number of swapcache pages in can_reclaim_anon_pages(). If the number is not zero, return true either. Moreover, add a new bit swapcache_only in struct scan_control to skip isolating anon pages that are not swapcache when only swapcache pages can be reclaimed to accelerate reclaim efficiency. Link: https://lore.kernel.org/linux-mm/14e15f31-f3d3-4169-8ed9-fb36e57cf578@huawei.com/ Signed-off-by: Liu Shixin Tested-by: Yosry Ahmed Reviewed-by: "Huang, Ying" Reviewed-by: Yosry Ahmed --- include/linux/swap.h | 6 ++++++ mm/memcontrol.c | 8 ++++++++ mm/vmscan.c | 29 +++++++++++++++++++++++++++-- 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index 456546443f1f..0318e918bfa4 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -669,6 +669,7 @@ static inline void mem_cgroup_uncharge_swap(swp_entry_t entry, unsigned int nr_p } extern long mem_cgroup_get_nr_swap_pages(struct mem_cgroup *memcg); +extern long mem_cgroup_get_nr_swapcache_pages(struct mem_cgroup *memcg); extern bool mem_cgroup_swap_full(struct folio *folio); #else static inline void mem_cgroup_swapout(struct folio *folio, swp_entry_t entry) @@ -691,6 +692,11 @@ static inline long mem_cgroup_get_nr_swap_pages(struct mem_cgroup *memcg) return get_nr_swap_pages(); } +static inline long mem_cgroup_get_nr_swapcache_pages(struct mem_cgroup *memcg) +{ + return total_swapcache_pages(); +} + static inline bool mem_cgroup_swap_full(struct folio *folio) { return vm_swap_full(); diff --git a/mm/memcontrol.c b/mm/memcontrol.c index e8ca4bdcb03c..c465829db92b 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -7567,6 +7567,14 @@ long mem_cgroup_get_nr_swap_pages(struct mem_cgroup *memcg) return nr_swap_pages; } +long mem_cgroup_get_nr_swapcache_pages(struct mem_cgroup *memcg) +{ + if (mem_cgroup_disabled()) + return total_swapcache_pages(); + + return memcg_page_state(memcg, NR_SWAPCACHE); +} + bool mem_cgroup_swap_full(struct folio *folio) { struct mem_cgroup *memcg; diff --git a/mm/vmscan.c b/mm/vmscan.c index 1080209a568b..e73e2df8828d 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -137,6 +137,9 @@ struct scan_control { /* Always discard instead of demoting to lower tier memory */ unsigned int no_demotion:1; + /* Swap space is exhausted, only reclaim swapcache for anon LRU */ + unsigned int swapcache_only:1; + /* Allocation order */ s8 order; @@ -613,10 +616,20 @@ static inline bool can_reclaim_anon_pages(struct mem_cgroup *memcg, */ if (get_nr_swap_pages() > 0) return true; + /* Is there any swapcache pages to reclaim? */ + if (total_swapcache_pages() > 0) { + sc->swapcache_only = 1; + return true; + } } else { /* Is the memcg below its swap limit? */ if (mem_cgroup_get_nr_swap_pages(memcg) > 0) return true; + /* Is there any swapcache pages in memcg to reclaim? */ + if (mem_cgroup_get_nr_swapcache_pages(memcg) > 0) { + sc->swapcache_only = 1; + return true; + } } /* @@ -2280,6 +2293,19 @@ static bool skip_cma(struct folio *folio, struct scan_control *sc) } #endif +static bool skip_isolate(struct folio *folio, struct scan_control *sc, + enum lru_list lru) +{ + if (folio_zonenum(folio) > sc->reclaim_idx) + return true; + if (skip_cma(folio, sc)) + return true; + if (unlikely(sc->swapcache_only && !is_file_lru(lru) && + !folio_test_swapcache(folio))) + return true; + return false; +} + /* * Isolating page from the lruvec to fill in @dst list by nr_to_scan times. * @@ -2326,8 +2352,7 @@ static unsigned long isolate_lru_folios(unsigned long nr_to_scan, nr_pages = folio_nr_pages(folio); total_scan += nr_pages; - if (folio_zonenum(folio) > sc->reclaim_idx || - skip_cma(folio, sc)) { + if (skip_isolate(folio, sc, lru)) { nr_skipped[folio_zonenum(folio)] += nr_pages; move_to = &folios_skipped; goto move;