From patchwork Tue Aug 22 02:49:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liu Shixin X-Patchwork-Id: 136452 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b82d:0:b0:3f2:4152:657d with SMTP id z13csp3416263vqi; Mon, 21 Aug 2023 22:17:49 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFV9ryTBZTJouWFg8u4T1EwT8GpAqhABno9NrJdS/hH8273gRvRhjRQErF4VAzkQdOEgDbs X-Received: by 2002:a05:6358:590e:b0:134:d78f:67bc with SMTP id g14-20020a056358590e00b00134d78f67bcmr7036043rwf.14.1692681468798; Mon, 21 Aug 2023 22:17:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692681468; cv=none; d=google.com; s=arc-20160816; b=Pt1p/8sdvUxtFZ9lsvq8fXKWEWLtN9NqgfA6Ddbc0+pxngETSPMwngw0rnex+lNey5 sCoc2eFtYsNx1uEHSNcld48DYxFBTGC/66S8cUsZucXmDTx1o/7tqC64s8wwquvt+ANV 29OvsjQ+Zj2z/pJOcEtEavWp2M0TFPZS+L4go2Fm2l0pvxQ2qjfR1vsQnB3UO4FSmRwH 3PcQDLrBhlSC5LhUfssoPGYF2HWS9aekB5A8x04BLE0ES0/srGEKTNsrbzuaLtGWdeEa 3YJO+oLRl4mbo7XoM/4Yq7nlhpMqiluVwf6tyfOFu4GAEmePYwcIM7MHSdz+dvqBPXSC nVtQ== 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=0FtwtddC9wtHSGf+iRKgWLC4T3po4JjM5fDH7RGmjQM=; fh=W2A6W2q3XQEgP8SwjvyI1IJO9SMivD0j9cbp9sHEDJA=; b=CIflqFm5zIHDPQR/GCV4/Jrd6JIUISWuSUgNoC4u66lK+4nc+j7NO6VqjQrPBEfJHS jgCayPYmt2KCkdwF+rMC3WIIGrrzbkYKfpVU8zejKiFEH+q+4ASRD0Ch4cBdUH+HQbEM qK+OpKlQ7XkbCd0KrAbmH1azTDZVKtD6WPcOuLmoyGpYkyYMgSbMjLSLIOirVj1xRsUI ObiSkUkV/a46ljL2faPtNUM4hljqfag4V9yMyaAt5JUQeaXvvtciZnqepREHpzSlahNR PdMfYZIedrCWskIcnehAtTj80s1L6cOows38bKzS20FU5Wgr/V1/e3bGl4bpFbebhRt8 +TUg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i12-20020a633c4c000000b0056a1a709797si5703987pgn.654.2023.08.21.22.17.33; Mon, 21 Aug 2023 22:17:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232157AbjHVBya (ORCPT + 99 others); Mon, 21 Aug 2023 21:54:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54812 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232108AbjHVBy3 (ORCPT ); Mon, 21 Aug 2023 21:54:29 -0400 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A3D06113; Mon, 21 Aug 2023 18:54:26 -0700 (PDT) Received: from dggpemm500009.china.huawei.com (unknown [172.30.72.55]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4RVC6K1X1rz1L9Kv; Tue, 22 Aug 2023 09:52:57 +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; Tue, 22 Aug 2023 09:54:24 +0800 From: Liu Shixin To: Johannes Weiner , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , Andrew Morton , CC: , , , Liu Shixin Subject: [PATCH v2] mm: vmscan: reclaim anon pages if there are swapcache pages Date: Tue, 22 Aug 2023 10:49:01 +0800 Message-ID: <20230822024901.2412520-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-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1774905163454356080 X-GMAIL-MSGID: 1774905163454356080 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. This problem can be fixed by checking number of swapcache pages in can_reclaim_anon_pages(). For memcg v2, there are swapcache stat that can be used directly. For memcg v1, use total_swapcache_pages() instead, which may not accurate but can solve the problem. Signed-off-by: Liu Shixin --- include/linux/swap.h | 6 ++++++ mm/memcontrol.c | 8 ++++++++ mm/vmscan.c | 12 ++++++++---- 3 files changed, 22 insertions(+), 4 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..3e578f41023e 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() || do_memsw_account()) + 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 7c33c5b653ef..bcb6279cbae7 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -609,13 +609,17 @@ static inline bool can_reclaim_anon_pages(struct mem_cgroup *memcg, if (memcg == NULL) { /* * For non-memcg reclaim, is there - * space in any swap device? + * space in any swap device or swapcache pages? */ - if (get_nr_swap_pages() > 0) + if (get_nr_swap_pages() + total_swapcache_pages() > 0) return true; } else { - /* Is the memcg below its swap limit? */ - if (mem_cgroup_get_nr_swap_pages(memcg) > 0) + /* + * Is the memcg below its swap limit or is there swapcache + * pages can be freed? + */ + if (mem_cgroup_get_nr_swap_pages(memcg) + + mem_cgroup_get_nr_swapcache_pages(memcg) > 0) return true; }