From patchwork Fri Sep 15 08:34:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liu Shixin X-Patchwork-Id: 13386467 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 9B4B9EE6433 for ; Fri, 15 Sep 2023 08:02:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B81AE8D0020; Fri, 15 Sep 2023 04:02:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B31848D0001; Fri, 15 Sep 2023 04:02:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A20A58D0020; Fri, 15 Sep 2023 04:02:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 8F45D8D0001 for ; Fri, 15 Sep 2023 04:02:28 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 607DDB43F1 for ; Fri, 15 Sep 2023 08:02:28 +0000 (UTC) X-FDA: 81238089576.25.FFB93E2 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf05.hostedemail.com (Postfix) with ESMTP id EC4D9100013 for ; Fri, 15 Sep 2023 08:02:24 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf05.hostedemail.com: domain of liushixin2@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=liushixin2@huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1694764946; 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=/mGdpSbwfreVKhjJ05qhHd6SBRk5l863rQGqpVKzt5Q=; b=QIcTNBqyci3Hg6aj8h1pP8CSf74OhA4Sph1XwnNdJk1QMFKcRXgTdl+qq95JGgOPzbPxfe YQFZjBmYVomp5C5bZ9KbPfzSyi7cTFQ5dCWx2v42cRwfBtIYcTz4phpF9IRsyLTyfygn8y h4jQ5AuKeyU/9wZ5KfYHg8Y3vMKceW8= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf05.hostedemail.com: domain of liushixin2@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=liushixin2@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694764946; a=rsa-sha256; cv=none; b=1i9DSkikJOSDRkc4syLNRQrZnmJ+XLjxCx4/f5VJqU+gcz6uATp4XYUVbeZYIqAZDh8M3m A+WLGIMK18zjWBYCbEuni5k/clNREHx8R2T5ymXe7vAekFuk1wySMFYJ43LxuIYyogGkVl S302qcUVBoGChVkQ7YaOq5iNI2prnrc= Received: from dggpemm500009.china.huawei.com (unknown [172.30.72.57]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4Rn5bh47ktztSrF; Fri, 15 Sep 2023 15:36:32 +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; Fri, 15 Sep 2023 15:40:40 +0800 From: Liu Shixin To: Andrew Morton , Yosry Ahmed , Huang Ying , Sachin Sant , Michal Hocko , Johannes Weiner , Kefeng Wang CC: , , Liu Shixin Subject: [PATCH v6] mm: vmscan: try to reclaim swapcache pages if no swap space Date: Fri, 15 Sep 2023 16:34:17 +0800 Message-ID: <20230915083417.3190512-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: dggems703-chm.china.huawei.com (10.3.19.180) To dggpemm500009.china.huawei.com (7.185.36.225) X-CFilter-Loop: Reflected X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: EC4D9100013 X-Stat-Signature: t75zf1w9e9t4ie18jht8b867uhc6mfoa X-Rspam-User: X-HE-Tag: 1694764944-178095 X-HE-Meta: U2FsdGVkX18GCABhi5cVCoiWsyBmd4BFzozDcUPzllAn/wXFSUJzkw+lGsQUbL8S2qvEyzkngJ2vuqFe4A4ZISk0TBUsnCed0XbHIR4l1vOPTYRK8hQuJCvhe4zr3E9EEcba1f6aL0DqhhZs3TCDb9fXwVuAQ40RAfxfZufwJj0CJW4fm8Br2WCRuQ5iTWcW/ZWeqI9w/2PDiaunNXkwJFrnBAlTM1NrLr/FDP9hEuIK4EjtQoH7eMzSHOXYdas50hXUdAAuLldbIHC0ZhDf761A/qwd09zJ2CXhPCcsYkIGh3RoMYH5d4BlzwTxzfbDTgd9Nzu8npxh3XiTMx9NOccftHGj77SxQ7x2aaR1MgYk/F3tCF/FLo4uB6jMWW8qBWrFFgwnvSBqOONHzexAXKi08F3iab18a0HS4gG+detuwYu+IjMt2EESyw/Vl2CA7Dfb18fX9YYiDVGOWRATRD5L1ke8iVSlHtX9CaWwhdWNUb4nDKNkxPT88mkAgzrN6SX5T9ntISxQ+ULYtHO/cExqeRKYSmvVy0vRYkDLXqp6s70woJ9cU2jHkqJ80Ec+Fkg+l6XyNpGMUvli+7ibKTkd66rFu/v91Kvt5mFAt7aH4afYMgR9GDVUbFV8+/4bRH5hmSsAU2GMj/M7xTsNsZRE9c/C+XLHt54Bdvt/bCb/WCvVxmywNbEVJj090v0arLztyxeGeIlcBffumCmhecvr+QRbrUD2kCbcvrdDcxv2nWuHoA0A3jMmolsZ/Mioa7BaFqSmOBcY6brrv5h2IlWYxGKiKO0uXZw9vauUzeI+z0YoRGy86/IBeEptLfKOMmqyQ6PoEoNSpDjCjE54LzTVcvG6QWpCu8/RVGi+5ytssA5PrrLPYk+aeN3H0XHlO1BO7U0s9goinxyZmQORWDCQo3OKWUb36kuWNie2mtp1BsT4py2EggG97XOBBC4vh8UZyklQgimTDmCGnGB 7Zd2RDhc XoBWXMhlNAhRP3phBu1Ls0nj4I7iJ15Ap/xTrOXHYLD+wOvbHggF/cI1MwrXIPmpQdHBVMHPYl6Sca+SWrgM7dSeiqqQPJ/a8dqF4RtJ71DUhMOvoKzSCHTw0x0k7+nP50pHH6dqo/4TcQm51DcoZc7LtnoVWU2K5fLaZ6mEz2ai7tZw47agvCi/+jxrua6yG6S6uNSi1BNnKuaNlZGw1zPSYAWSYcOIGZ3jTLyKyGgJOVtmtVMperDj9uwo1cfjjL5Np1Gc5ifGM51EnxqBVyZMGog== 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 [1]. Fix it by checking number of swapcache pages in can_reclaim_anon_pages(). If the number is not zero, return true and set swapcache_only to 1. When scan anon lru list in swapcache_only mode, non-swapcache pages will be skipped to isolate in order to accelerate reclaim efficiency. However, in swapcache_only mode, the scan count still increased when scan non-swapcache pages because there are large number of non-swapcache pages and rare swapcache pages in swapcache_only mode, and if the non-swapcache is skipped and do not count, the scan of pages in isolate_lru_folios() can eventually lead to hung task, just as Sachin reported [2]. By the way, since there are enough times of memory reclaim before OOM, it is not need to isolate too much swapcache pages in one times. [1]. https://lore.kernel.org/lkml/CAJD7tkZAfgncV+KbKr36=eDzMnT=9dZOT0dpMWcurHLr6Do+GA@mail.gmail.com/ [2]. https://lore.kernel.org/linux-mm/CAJD7tkafz_2XAuqE8tGLPEcpLngewhUo=5US14PAtSM9tLBUQg@mail.gmail.com/ Signed-off-by: Liu Shixin Tested-by: Yosry Ahmed Reviewed-by: "Huang, Ying" Reviewed-by: Yosry Ahmed --- v5->v6: Fix NULL pointing derefence and hung task problem reported by Sachin. include/linux/swap.h | 6 ++++++ mm/memcontrol.c | 8 ++++++++ mm/vmscan.c | 24 ++++++++++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/include/linux/swap.h b/include/linux/swap.h index 493487ed7c38..19f30a29e1f1 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -658,6 +658,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) @@ -680,6 +681,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 34bae9e63ff2..26ae2e08fd9a 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -7573,6 +7573,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 3df0e2a59052..48c092cc6088 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -136,6 +136,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; @@ -319,10 +322,22 @@ 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) { + if (sc) + 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) { + if (sc) + sc->swapcache_only = 1; + return true; + } } /* @@ -1642,6 +1657,15 @@ static unsigned long isolate_lru_folios(unsigned long nr_to_scan, */ scan += nr_pages; + /* + * Count non-swapcache too because the swapcache pages may + * be rare and it takes too much times here if not count + * the non-swapcache pages. + */ + if (unlikely(sc->swapcache_only && !is_file_lru(lru) && + !folio_test_swapcache(folio))) + goto move; + if (!folio_test_lru(folio)) goto move; if (!sc->may_unmap && folio_mapped(folio))