Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp3546931rdh; Mon, 27 Nov 2023 18:00:48 -0800 (PST) X-Google-Smtp-Source: AGHT+IECU5eCQjRL0mamWNVOnDPdMXx+mhboO5rt5AeXMyql1EuXc/BYvYyPCAtSjpG5IKXa2fI1 X-Received: by 2002:a05:6830:134d:b0:6af:95f9:7adc with SMTP id r13-20020a056830134d00b006af95f97adcmr14898335otq.14.1701136847940; Mon, 27 Nov 2023 18:00:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701136847; cv=none; d=google.com; s=arc-20160816; b=dLBhgrjYvVwic+FZulPsZblBQMLfFVP+MCBribWMWyP889ikDnKKO13khH3K8KdcSP vNBqbSSm6uxuVZ/+m0NGWWgTeRcjmafeVW849LPyZ5HpRvHHQ20YRW15rSg+rUPgICTO rYdj13xbgZ+/59+OFrZl41zmqPs24UZZYB+AH6CV7i7ndGYe9eBUhMgls/doIA6x5dxr geJFpXamLaQEY75FhFts4BxdhmiLgP0Gxz/tiKpYEkRFNc3WMB1Tfgwr4netA5sxQKgW X31B59DtshxlBV4IgzvuVgkDitJTH1Lr5WscmngnEibiNbm7t88//72dqYDLbXTb+BTq 9oKQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to :mime-version:user-agent:date:message-id:from:cc:references:to :subject; bh=+B+PaYdSRDNzyBQNqp1FUhdeX6dWRSgfRj1EUs6LdS8=; fh=QwI2t2bNEyXIPoZ0bBugek9ZXU4FnsWgS9fRFTT+KyI=; b=wgCp1+d59PxwfGvHVaeVL3JYYPunCaZDHSw5AeMbR3hgzCv3j4oKkd9kyxCkTARyZB A/CEm3TdoskBsroZkoZTJqAUClmGoyApPtEphmAatktSOE481ao7/7es2Ol+M8L6IL3o 4fghbVCf0rFSP6z8C3fqOMj9+LTJbzHHg84FosrIATW6YtcxdjDGT5QAR7aeGTfQviIb qwOulxoqNBRMtrNZNSUi+knT6YemZJV2Bd4RagcD4e2B/2qpSIal9+5A4fDaTOcGF0bQ PixoI2F56VS5hbAvLpV0C/iYPqjNbNkvIGFllWDEB2zOkSmWMjZkcVk971kZ3tk74FTm aAZA== 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:1 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 morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id z18-20020a9d7a52000000b006bd3eda3c4esi4346226otm.233.2023.11.27.18.00.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 18:00:47 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 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 morse.vger.email (Postfix) with ESMTP id 0B6F980A134E; Mon, 27 Nov 2023 18:00:09 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234375AbjK1B7x (ORCPT + 99 others); Mon, 27 Nov 2023 20:59:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33732 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231954AbjK1B7w (ORCPT ); Mon, 27 Nov 2023 20:59:52 -0500 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6EBBE1AA for ; Mon, 27 Nov 2023 17:59:57 -0800 (PST) Received: from dggpemd200004.china.huawei.com (unknown [172.30.72.56]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4SfQX920j6zShKJ; Tue, 28 Nov 2023 09:55:37 +0800 (CST) Received: from [10.174.179.24] (10.174.179.24) 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.28; Tue, 28 Nov 2023 09:59:54 +0800 Subject: Re: [PATCH v10] mm: vmscan: try to reclaim swapcache pages if no swap space To: Chris Li References: <20231121090624.1814733-1-liushixin2@huawei.com> CC: Yu Zhao , Andrew Morton , Yosry Ahmed , Huang Ying , Sachin Sant , Michal Hocko , Johannes Weiner , Kefeng Wang , , From: Liu Shixin Message-ID: Date: Tue, 28 Nov 2023 09:59:54 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.7.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit X-Originating-IP: [10.174.179.24] 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-Spam-Status: No, score=-2.6 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.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 (morse.vger.email [0.0.0.0]); Mon, 27 Nov 2023 18:00:09 -0800 (PST) On 2023/11/24 1:19, Chris Li wrote: > Hi Shixin, > > On Tue, Nov 21, 2023 at 12:08 AM Liu Shixin wrote: >> 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]. > Just out of my curiosity, in your usage case, how much additional > memory in terms of pages or MB can be freed by this patch, using > current code as base line? My testcase is in a memory cgroup with memory limit of 10MB, the memory can be freed is only about 5MB. > Does the swap cache page reclaimed in swapcache_only mode, all swap > count drop to zero, and the only reason to stay in swap cache is to > void page IO write if we need to swap that page out again? Yes. > >> 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. > Here you said non-swapcache will be skipped if swapcache_only == 1 > >> 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 > Here you suggest non-swapcache pages will also be scanned even when > swapcache_only == 1. It seems to contradict what you said above. I > feel that I am missing something here. The swapcache pages and non-swapcache pages are both in anon lru. So when scan anon pages, then non-swapcache pages will also be scanned. In isolate_lru_folios(), if we select to put non-swapcache pages in folios_skipped list, the scan of anon list will running until finding enough swapcache pages, this will waste too much time. To avoid such problem, after we scan enough anon pages, even if we don't isolate enough swapcache pages, we have to stop. > >> is skipped and do not count, the scan of pages in isolate_lru_folios() can > Can you clarify which "scan of pages", are those pages swapcache pages > or non-swapcache pages? I mean scan of anon pages, include both swapcache pages and non-swpacache pages. > >> 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 >> --- >> v9->v10: Use per-node swapcache suggested by Yu Zhao. >> 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. >> >> mm/vmscan.c | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- >> 1 file changed, 49 insertions(+), 1 deletion(-) >> >> diff --git a/mm/vmscan.c b/mm/vmscan.c >> index 506f8220c5fe..1fcc94717370 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; >> >> @@ -308,10 +311,36 @@ static bool can_demote(int nid, struct scan_control *sc) >> return true; >> } >> >> +#ifdef CONFIG_SWAP >> +static bool can_reclaim_swapcache(struct mem_cgroup *memcg, int nid) >> +{ >> + struct pglist_data *pgdat = NODE_DATA(nid); >> + unsigned long nr_swapcache; >> + >> + if (!memcg) { >> + nr_swapcache = node_page_state(pgdat, NR_SWAPCACHE); >> + } else { >> + struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat); >> + >> + nr_swapcache = lruvec_page_state_local(lruvec, NR_SWAPCACHE); >> + } >> + >> + return nr_swapcache > 0; >> +} >> +#else >> +static bool can_reclaim_swapcache(struct mem_cgroup *memcg, int nid) >> +{ >> + return false; >> +} >> +#endif >> + >> static inline bool can_reclaim_anon_pages(struct mem_cgroup *memcg, >> int nid, >> struct scan_control *sc) >> { >> + if (sc) >> + sc->swapcache_only = 0; >> + > Minor nitpick. The sc->swapcache_only is first set to 0 then later set > to 1. Better use a local variable then write to sc->swapcache_only in > one go. If the scan_control has more than one thread accessing it, the > threads can see the flicker of 0->1 change. I don't think that is the > case in our current code, sc is created on stack. There are other > minor benefits as The "if (sc) test" only needs to be done once, one > store instruction. > > Chris Thanks for your advice. If finally decide to use this patch, I will revise it. >> if (memcg == NULL) { >> /* >> * For non-memcg reclaim, is there >> @@ -330,7 +359,17 @@ static inline bool can_reclaim_anon_pages(struct mem_cgroup *memcg, >> * >> * 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 in this node? */ >> + if (can_reclaim_swapcache(memcg, nid)) { >> + if (sc) >> + sc->swapcache_only = 1; >> + return true; >> + } >> + >> + return false; >> } >> >> /* >> @@ -1642,6 +1681,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 >> >> > . >