From patchwork Wed Nov 15 05:01:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liu Shixin X-Patchwork-Id: 13456212 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 BC12CC07548 for ; Wed, 15 Nov 2023 04:24:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CFE406B02DB; Tue, 14 Nov 2023 23:24:02 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C874C6B02EC; Tue, 14 Nov 2023 23:24:02 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B273D6B0320; Tue, 14 Nov 2023 23:24:02 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 9FFAB6B02DB for ; Tue, 14 Nov 2023 23:24:02 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 6CE454094A for ; Wed, 15 Nov 2023 04:24:02 +0000 (UTC) X-FDA: 81458895924.18.61EE52F Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by imf16.hostedemail.com (Postfix) with ESMTP id 4BDAE18000F for ; Wed, 15 Nov 2023 04:23:58 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf16.hostedemail.com: domain of liushixin2@huawei.com designates 45.249.212.189 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=1700022240; 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=xRRJQ1mkb2cIVAgO94fsLX4WHWLsLBXsMfevvD6Qklg=; b=ioKgPPJe6ymUVwa1cN2yZJZ4gyWTWV5WqpuAtsfzeAMvo5PXOtb5X5WYha5LlGwd5reiB+ v8KIZ7V1oB8wdSaO0d+rl8tIGb2at/sfuHqegJFXDtEgJBvKmCdr8ikOCyFnYlXlqkjipC VpEBn/Y+c9Jtz8fYL7C6Yti77vZK1jw= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf16.hostedemail.com: domain of liushixin2@huawei.com designates 45.249.212.189 as permitted sender) smtp.mailfrom=liushixin2@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700022240; a=rsa-sha256; cv=none; b=MNX/O9MqbB+aYtEaZ7ETMWAwdjToZ02JsEezxNIJtHRiE7KvQxagONsB8im2V6i/EQfqXT fiew6BjGXNcqlnrkEpVLWbiUbtBxwhAlDC6a1KScJiW1yW8a2aM3Fbcigp+Ymsoemf43vw 5EOVvGQp6a2i9Po09SJF7JCX2zKHK+o= Received: from dggpemd200004.china.huawei.com (unknown [172.30.72.54]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4SVTsp3CCwzMmn5; Wed, 15 Nov 2023 11:58:22 +0800 (CST) Received: from huawei.com (10.175.113.32) by dggpemd200004.china.huawei.com (7.185.36.141) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.2.1258.23; Wed, 15 Nov 2023 12:02:57 +0800 From: Liu Shixin To: Andrew Morton , Yosry Ahmed , Huang Ying , Sachin Sant , Michal Hocko , Johannes Weiner , Kefeng Wang CC: , , Liu Shixin Subject: [PATCH v9] mm: vmscan: try to reclaim swapcache pages if no swap space Date: Wed, 15 Nov 2023 13:01:23 +0800 Message-ID: <20231115050123.982876-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 dggpemd200004.china.huawei.com (7.185.36.141) X-CFilter-Loop: Reflected X-Rspamd-Queue-Id: 4BDAE18000F X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: 8qd8itnedtggxkqonuaz3f8c6i31axci X-HE-Tag: 1700022238-73946 X-HE-Meta: U2FsdGVkX19YpMH9/j2aslMZLO93Gzla0blLnlvQdEft5jWrexlv79LDXZHsR8PNHeUM8qSaMcXXUF2n+oPzZRqkGrkxPBLQ+voy4mlCPQtKKC0WloBxT42rNjY78UOvRm12rWFywgFodwDTs1i2kWurPCLhkM4FO1vMQqAKnIZVlYxcSKPjOXXwYOWYbTx55hw4XZ/CUoQF7mVMSFySIAiHLqFqTsmVTy+syCcGcFFQFygGAeP5ozG8gPXuCjbN3w9DsXJlY5XLdg3t3O2Usiqz+UhISjkzAaXnJBwsBq8gAVChwtx56fISB+IKwArvPkZW0VvyySxCYSG34UZ6Z9Inv5dC/nKs9opYpsMSFvykF6K/R1IGbErqmWcHMi3kbL/iybY6++SVDU7taolZiCqw7fUe63xtjiIMlVeXqD9Vqp6N7IHaMWnMAwUEK2pkdIA39KRfUVhd8ainJzH4ifmKZ4QZO23WO4mvlvQlbQM0NN28xuqMHDGigpcLIgWgsP1f2Gm/egbLZP5X5dq3yHN2up/6rjPLFbw9nZ9iIv2PkPGHEegfucalPULPKb4x5RY/pS0dt7X94FPmxdmxb/MO5JK6WCWqMcPsx8Flh8cQca/EWd9Ww/KpzS8iLY+GTEpWXZ7wQ8hbejUCVL7Upj+mnN58jYIOA1HGpcEhXhc5jHs5DqNIBpzAwovnmUBdHdFnhQVU3U7GraFdGSls+dBn2CK3/P8W3SaWoJOWsxN9wncyJ9mn+qr3kOFaGKToN938zQ4HAg/BM9VEur5tAm+oMkll0fvTb1SF4xPW/0x0rhTzJhh/gVGX6s27T0giET4pz9csL1Us1hMmUJSrtrqaNdKQ++AtoClcj878ti/DWg8vGk0Bdk++N6POB9JCHG4ySSmUnHJJmJIeAolc4wWyOqwc9ojFrcF9MAlpbcC+gLayXSZPKb5+Xep5V80pjrlLrkIx6q3W7T4kDZw ovj1tnIc 01H3ro9Ue5TA1CPN7kno+XwR3Tw5TPBOf5yn54gJSlZ7RfYSgOK3Ef7nU9pVtaPFYLIdPALSMwsMH3QY+juOnF0BJzhZaI7QOsK6FyeoYcUBHuBrs+9j5oNwTMDuHUNwU/xLBGIkJd3UDHm5XLjRaE5XJAD43I49jFbYjA9ukuirRi9ZW8F0QU4eCof4cjSA/SZShA3kgthB/bvNuaKLQGKIIreMeU6yx/bg8IcmlRD74jCn4LrsikVMriH3k/ifmcMlDE1GRuhmwcRLE8zuwM4FzXQ== 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: List-Subscribe: List-Unsubscribe: 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 --- v8->v9: Move the swapcache check after can_demote() and refector can_reclaim_anon_pages() a bit. v7->v8: Reset swapcache_only at the beginning of can_reclaim_anon_pages(). v6->v7: Reset swapcache_only to zero after there are swap spaces. 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 | 47 ++++++++++++++++++++++++++++++++------------ 3 files changed, 48 insertions(+), 13 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index f6dd6575b905..3ba146ae7cf5 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -659,6 +659,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) @@ -681,6 +682,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 774bd6e21e27..a76ec540d4a3 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -7865,6 +7865,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 506f8220c5fe..62a1c75f74ad 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; @@ -312,25 +315,34 @@ static inline bool can_reclaim_anon_pages(struct mem_cgroup *memcg, int nid, struct scan_control *sc) { - if (memcg == NULL) { - /* - * For non-memcg reclaim, is there - * space in any swap device? - */ - if (get_nr_swap_pages() > 0) - return true; - } else { - /* Is the memcg below its swap limit? */ - if (mem_cgroup_get_nr_swap_pages(memcg) > 0) - return true; - } + if (sc) + sc->swapcache_only = 0; + + /* + * For non-memcg reclaim, is there space in any swap device? + * Or is the memcg below its swap limit? + */ + if ((!memcg && get_nr_swap_pages() > 0) || + (memcg && mem_cgroup_get_nr_swap_pages(memcg) > 0)) + return true; /* * The page can not be swapped. * * Can it be reclaimed from this node via demotion? */ - return can_demote(nid, sc); + if (can_demote(nid, sc)) + return true; + + /* Is there any swapcache pages to reclaim? */ + if ((!memcg && total_swapcache_pages() > 0) || + (memcg && mem_cgroup_get_nr_swapcache_pages(memcg) > 0)) { + if (sc) + sc->swapcache_only = 1; + return true; + } + + return false; } /* @@ -1642,6 +1654,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))