From patchwork Wed Aug 30 03:56:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liu Shixin X-Patchwork-Id: 13369919 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 51444C83F12 for ; Wed, 30 Aug 2023 03:02:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7AB9E680001; Tue, 29 Aug 2023 23:01:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 75BA78E000B; Tue, 29 Aug 2023 23:01:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 64A91680001; Tue, 29 Aug 2023 23:01:59 -0400 (EDT) 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 550488E000B for ; Tue, 29 Aug 2023 23:01:59 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 28F6416040C for ; Wed, 30 Aug 2023 03:01:59 +0000 (UTC) X-FDA: 81179271558.05.FD0C8C5 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by imf26.hostedemail.com (Postfix) with ESMTP id 6A547140002 for ; Wed, 30 Aug 2023 03:01:53 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf26.hostedemail.com: domain of liushixin2@huawei.com designates 45.249.212.255 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=1693364517; 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=47ddbhxzKYutr4ap56W+rUYyM0EW+CxTOODHGx5QfNo=; b=v1TA2OSNOSLFtI/jJYNsZDFSBQVA2lTXPoo+4sJlTqRyvOrZZ21KWOXeOHvKUnzJyijqA7 oBmWR01H+T/7yZzYmuOQqnUwUZ5wk8JxN5s7IIahMeVFMI/ojkG+ljkg/kGG8nFW47BQss A7117yooIkrWQu3P4JCVjH2cTUTGSE0= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf26.hostedemail.com: domain of liushixin2@huawei.com designates 45.249.212.255 as permitted sender) smtp.mailfrom=liushixin2@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1693364517; a=rsa-sha256; cv=none; b=7iU4hp1ullw9aCJb7ukncHJjhp/yxg4aa449ukEGiUrcsiaR1on/+4XhDBdM5Jw4zNFX7a aiO7lbVd8tzwpOp5iD3NgiQzEBHqiBfdjWPkOXLfh+dB1XH+ycJZhvsEwvNLbSYVJ7SRpy OQB/odkwebMNXDxJEfj2dfzoHFyy3P8= Received: from dggpemm500009.china.huawei.com (unknown [172.30.72.55]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4Rb8DB6NnLz1L9B2; Wed, 30 Aug 2023 11:00:10 +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; Wed, 30 Aug 2023 11:01:48 +0800 From: Liu Shixin To: Yosry Ahmed , Huang Ying , Michal Hocko , Johannes Weiner , Roman Gushchin , Shakeel Butt , Muchun Song , Andrew Morton , CC: , , , Liu Shixin Subject: [PATCH v5] mm: vmscan: try to reclaim swapcache pages if no swap space Date: Wed, 30 Aug 2023 11:56:00 +0800 Message-ID: <20230830035600.1656792-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: 6A547140002 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 4kfq9gqoww3xmqw4otwzsgxguxiksbsw X-HE-Tag: 1693364513-805165 X-HE-Meta: U2FsdGVkX18jzFCFCvDcx19c4Rl6FK2Wis3pjS7x1FO0RTlYCd5RruRZSIgIwpTZI45Uk/j/5YQfh5VjKAXWZu1wIyCr+CTlDTssrzhT/j3icjW91+YO1gJ6XF0h7UZ751dwOYzy1XUGlHTo2R2sMAv+gZUvzH2wdYr5K2tpOlIdWerSAdA9iiIKpAZwqYqU9yjabDUYBcPdpqMoUnpDFgp7u3eKFs4uxsqHPkel+7Q0Yjb857EzIV5nElITT/JyhMlIeAqpDasnM/T+2tX323Co/8FY1+t6vHgOWIAya8lYOYW+oDfY2mTu0FoHXde37nyHdIOYPBT4ZeBnwshGd/Joor9IxPIVYvg3eJlbVGDYUhQ/3TjQaNYXm/f/q5AIehde8nStnMREbeRfCbrkuhMJiIYLSaBrwp/5MXXu8AxITuZc4ShmQVfZATjKiuPBJB+uIfqymZ/zuJZu3A+WiKHhRyqkeTXRAouZ+okDn3561de5FLZOoc6OpmzHzHShuUel5UJ1UyL/ztjHyjal8B6dtuBtrf1GFEH435IdAd24cUgGvwmuvxJMX7hadoq21InRLZjATB3SpZMIfFVWoWaMxAzH7rVqAukZ5BM7wMTU77uzhLRHwI8Auv51nK5kIjSZep1VSR4Ut4CaMNMAIDjHdUex5z5UNlwHYbiFRxJu54XWQYO7cWPvzLZw/q1WNI0UDF05T6ZGkHiR4O2PR4b0ZaPRunHLy0/iTTcQtLjSwpgzYBQyk6h1PXH1bYVx00RrRs+iqTcXlZJfXAkOzz71ev3lzx19rlw+yk4OKqiMqNPG5gEHMSqsFV2yULqFyab4Oce80JgpNLd2soBpEY1DV6r1kPewwRDQTtni2Zj2uE55xG0AKAFlqpPIhEsdS8OPR91Sr0O8iCo4fGgJJGkjF+Is8cYrs/2rxHVX1wMxWQ0NVu4uXFkF2E7bf/HrE+FOoANHmbtu1XZeUZl AKv1n34T 9VG6pS6EczlA1wg1FNEZ3c9eSXDYozzlbabycW20NtigRDxahHg0jBr3p8/oqbl8NGEMCUjmoZ4cxFjFhzzuMjSe4YHhi+7gy21ez08O/y4tNq+OMq31htYbNoxlkAV1Bo1Yg96vUcnKwaWg91Wk2kz19MgYpsRTx/Un7VFZ3JDluAZ31c2OfEqX6LkhpoZf4acK0+6GMtdJ3C3hVadG5EYDHOjVEVNtTly0dmEvQmqOXWYwCd/3VdELxvRQwlycP+g8VkPkr0p53g0I2B8irXAW5pmbAxF0zc7UX5dwVVXFMRxUFI25VK/9tPj42BoaRmX0X 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. Fix it by checking number of swapcache pages in can_reclaim_anon_pages(). If the number is not zero, return true either. Moreover, add a new bit swapcache_only in struct scan_control to skip isolating anon pages that are not swapcache when only swapcache pages can be reclaimed to accelerate reclaim efficiency. Link: https://lore.kernel.org/lkml/CAJD7tkZAfgncV+KbKr36=eDzMnT=9dZOT0dpMWcurHLr6Do+GA@mail.gmail.com/ Signed-off-by: Liu Shixin Tested-by: Yosry Ahmed Reviewed-by: "Huang, Ying" Reviewed-by: Yosry Ahmed --- v3->v4: Add describe and link about how to reproduce the problem. v4->v5: Add Reviewed-by and reproducer link. include/linux/swap.h | 6 ++++++ mm/memcontrol.c | 8 ++++++++ mm/vmscan.c | 29 +++++++++++++++++++++++++++-- 3 files changed, 41 insertions(+), 2 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..c465829db92b 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()) + 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 1080209a568b..e73e2df8828d 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -137,6 +137,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; @@ -613,10 +616,20 @@ 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) { + 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) { + sc->swapcache_only = 1; + return true; + } } /* @@ -2280,6 +2293,19 @@ static bool skip_cma(struct folio *folio, struct scan_control *sc) } #endif +static bool skip_isolate(struct folio *folio, struct scan_control *sc, + enum lru_list lru) +{ + if (folio_zonenum(folio) > sc->reclaim_idx) + return true; + if (skip_cma(folio, sc)) + return true; + if (unlikely(sc->swapcache_only && !is_file_lru(lru) && + !folio_test_swapcache(folio))) + return true; + return false; +} + /* * Isolating page from the lruvec to fill in @dst list by nr_to_scan times. * @@ -2326,8 +2352,7 @@ static unsigned long isolate_lru_folios(unsigned long nr_to_scan, nr_pages = folio_nr_pages(folio); total_scan += nr_pages; - if (folio_zonenum(folio) > sc->reclaim_idx || - skip_cma(folio, sc)) { + if (skip_isolate(folio, sc, lru)) { nr_skipped[folio_zonenum(folio)] += nr_pages; move_to = &folios_skipped; goto move;