Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp4406663rdb; Fri, 15 Sep 2023 00:43:38 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE+bGWdi8QcTolmYiPeApHfpLXY2hSLI3U+bCwhQk5fdj/pzCiCtKbBLGmCxq30KqZmi8O/ X-Received: by 2002:a1f:4d81:0:b0:496:3a98:6c57 with SMTP id a123-20020a1f4d81000000b004963a986c57mr854761vkb.4.1694763818100; Fri, 15 Sep 2023 00:43:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694763818; cv=none; d=google.com; s=arc-20160816; b=QAo0VsM8AFIJa1YUVz4LLps+fScxRuHJUb/V4g9lwtJbhnaeH+6rMF+8BLTuHuKW9p QzGvCCPjllNlPBXRVrl52i1xkn1pLZc7q9GEVDIvQR3s1MjjAb4vNmtd+cWP/Q4ZFzHo Ft9Bi2ZMy4nHAeT3d2zQuy70Vxnq7flSsfhUdqzW6x5l8PhPBssNiimmDrc/3hZpQQrY Q0fxVPwvlP1ZxIXYfhcwmNQrUPDtdp2RWakFGVAIDRT3FduOE21IMfQg+/yGmDgk0c7R DSgzvtrh1bu6SYPicgwTO0mclcoZfh0oSHjxInwqcyuF/NEkqUBNokyAEExrs+Aw/7Ua ZXUw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=/mGdpSbwfreVKhjJ05qhHd6SBRk5l863rQGqpVKzt5Q=; fh=fpDLd387pmICRcjQLRWdh3NNt5NdlVYqbDAniWhOgjE=; b=NaSqKODdIbV3PFGppaBK8hbpqefBE11FZZ7F0COHK+o5dCCOEBY9afGswUkb3YV5tQ G4YFMAKla4wMtxuP5GzKLiB/Dyc8GYIx59Xmv+DdSzRtBFAHVKdWjWehHid1XfGQRjKM SnpnPNXgUvaImGvq6FgRFXRhQ19FZmtbelOX/DYiVRSLkELPRzsvHE9uPhsyJ8gBi6cE KGSYAsq7n441PXjSrak+Nga7K+bdl98u4NqmDWJIzopd73yuSCCvkyG41d7dFvOBSPr2 tmDX8h8/fjixZjqRFWJgXQWVbCbYmHS7qNk0vQXzVx5Jo/QKuNxbgbYdZI2hjna6hHdE xTvQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id p23-20020a056a000a1700b0068e29a05faesi3012411pfh.285.2023.09.15.00.43.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Sep 2023 00:43:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id DFDF0829C9BA; Fri, 15 Sep 2023 00:41:02 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232411AbjIOHkt (ORCPT + 99 others); Fri, 15 Sep 2023 03:40:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37966 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232125AbjIOHkt (ORCPT ); Fri, 15 Sep 2023 03:40:49 -0400 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 812CB98 for ; Fri, 15 Sep 2023 00:40:43 -0700 (PDT) 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 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII 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-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Fri, 15 Sep 2023 00:41:03 -0700 (PDT) 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)) -- 2.25.1