From patchwork Tue Aug 22 02:49:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liu Shixin X-Patchwork-Id: 13360072 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 C9EA0EE49A6 for ; Tue, 22 Aug 2023 01:54:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3DFE494001D; Mon, 21 Aug 2023 21:54:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 341E1940008; Mon, 21 Aug 2023 21:54:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2099294001D; Mon, 21 Aug 2023 21:54:32 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 0D7D0940008 for ; Mon, 21 Aug 2023 21:54:32 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id CBD2CC017A for ; Tue, 22 Aug 2023 01:54:31 +0000 (UTC) X-FDA: 81150071142.25.1E873EC Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by imf19.hostedemail.com (Postfix) with ESMTP id 391531A0006 for ; Tue, 22 Aug 2023 01:54:28 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=none; spf=pass (imf19.hostedemail.com: domain of liushixin2@huawei.com designates 45.249.212.255 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=1692669270; 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=0FtwtddC9wtHSGf+iRKgWLC4T3po4JjM5fDH7RGmjQM=; b=IDTkS1oMfoQVNPEoaKfecuRyN7ukabjimGw6BQLNcn5rYp9zzvItqef27OWyTBn98q32XM eFjJNVwVbp6MxeTC8EEftcfUPgPFbZpHRwfPnLLzlUYi1OoGEat62H19UBYpnutstuKax7 m86vCy+MIGFGqvb/v1YPQuNECdQHbhk= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1692669270; a=rsa-sha256; cv=none; b=SbxjbRhilNTtjj8sDyNDxUs8Id3K1nIxVj/IvLHuZCQKg7kq9CE5CaS/ELAR1a2jfAlfx0 WdZ3YWxbB8R6yon2ug+RNvhlHPK68OdIyx0GiwYF2Y9OG4GusURt+uA9mMtd7ci+rwI1kz icL2I47XeStIfXwVEEmFxQiaGx8iHiA= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=none; spf=pass (imf19.hostedemail.com: domain of liushixin2@huawei.com designates 45.249.212.255 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.55]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4RVC6K1X1rz1L9Kv; Tue, 22 Aug 2023 09:52:57 +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, 22 Aug 2023 09:54:24 +0800 From: Liu Shixin To: Johannes Weiner , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , Andrew Morton , CC: , , , Liu Shixin Subject: [PATCH v2] mm: vmscan: reclaim anon pages if there are swapcache pages Date: Tue, 22 Aug 2023 10:49:01 +0800 Message-ID: <20230822024901.2412520-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: dggems705-chm.china.huawei.com (10.3.19.182) To dggpemm500009.china.huawei.com (7.185.36.225) X-CFilter-Loop: Reflected X-Rspamd-Queue-Id: 391531A0006 X-Rspam-User: X-Stat-Signature: e73d5yg9tip89og4j9bawb4s84szifc7 X-Rspamd-Server: rspam03 X-HE-Tag: 1692669268-61698 X-HE-Meta: U2FsdGVkX191V+EZUCMOoaG/6xBrRpHOVdkW/Vu/B4A2cKLhRRxPvw570cLcLKPWGQ+GmsHfevMX4nxIB5uQaswlRprdoIe3C38pJkRs+60/cwyXaChrWLhVEqJLP3ursMV16QxEPcHpYV9Wsul5XkOCq++fa99xrBpDdTAQoeXkmBobnAgmo556icI4EoRz++f2QqvLQ62L6MyqAMv/HSBy1qJU6fIUBaBfeYErZGHXA8/4PIYoWVgN26xdWaVScAzKNtuOLkulIrjG8wHPWtH+Byj7z8rtK6RFRxw/VR7Aqu4+K9cEdusQqG64ivMoBA7vKMb13th6wAV2AT8oAgk7HF8cF5RuHBRS3fJ5wt2WYAkweh9zavXE01W9vKW9Nz8Hv+pAIAGTuu3OzTK5DK4g9PxlFxAKI7B9WFOxAxLekTldSiriT8MmwiKHqO6cvNfoU43+qx1KQa+scfLx69pa/Ytmrku6dhphbDe1RjwwV2yew0AhSNgsHJsmiR7icR2oqnFL6H966CUPnfqQBUhlbEN9mC7AkYHnP1/Z88WbqLtjhyGimDfNNDkHfcPHARfdcFabXPf+1V0U4T3/yVT2Al9bWlEI/tLCxYRAXrISB7BfoKEl30dEfhw07bBAhj4h9C9RB6ywIVQgj9Nf9dBZR6XgMLDYEPn2xT/vTGfrjoaBS5SlpjcHOSO2UdfgVWNt1ronDXB8vurFS5TpRaYLWrJMtkyBNObEUrdP2ov2qG5uDtlOiwPKtSos4eNRz4MrpHb4jraUjye5huvQ9M2VHYdbUTJhtMCOh6gzePHp5ANYmfURX0XWrWa18BF7rocGjh3vNGkldDR5ULHYzfjVRnYwCEh81Lw8LsqqoYPjoc9i4LOqHVpZYacPRgKkpv9BazXgULoe9ES/Sm5ibwYkzA8kTJ2K5pN7k/dFedvFe7+T0A+Blv/8hhHXW0zOjZInxgqy4u5dhbqUJpW 7qRcoM+d 4Atche2Qh5wMVbENhx29iBk4OIj4YZeLyZ+wZ3o3rTRQrnkdiYT/swu3GcVMAG30CjtD2rH0efpNVi7PyR/uMjKN7+uH9aswbBtHfhG3eYlnj/HOwIZNprbsdGeuf509Vjm8LXUSulwQyX6E= 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. This problem can be fixed by checking number of swapcache pages in can_reclaim_anon_pages(). For memcg v2, there are swapcache stat that can be used directly. For memcg v1, use total_swapcache_pages() instead, which may not accurate but can solve the problem. Signed-off-by: Liu Shixin --- include/linux/swap.h | 6 ++++++ mm/memcontrol.c | 8 ++++++++ mm/vmscan.c | 12 ++++++++---- 3 files changed, 22 insertions(+), 4 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..3e578f41023e 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() || do_memsw_account()) + 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 7c33c5b653ef..bcb6279cbae7 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -609,13 +609,17 @@ static inline bool can_reclaim_anon_pages(struct mem_cgroup *memcg, if (memcg == NULL) { /* * For non-memcg reclaim, is there - * space in any swap device? + * space in any swap device or swapcache pages? */ - if (get_nr_swap_pages() > 0) + if (get_nr_swap_pages() + total_swapcache_pages() > 0) return true; } else { - /* Is the memcg below its swap limit? */ - if (mem_cgroup_get_nr_swap_pages(memcg) > 0) + /* + * Is the memcg below its swap limit or is there swapcache + * pages can be freed? + */ + if (mem_cgroup_get_nr_swap_pages(memcg) + + mem_cgroup_get_nr_swapcache_pages(memcg) > 0) return true; }