From mboxrd@z Thu Jan 1 00:00:00 1970 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 8E879C3DA6F for ; Thu, 24 Aug 2023 18:32:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 28BAF2800A4; Thu, 24 Aug 2023 14:32:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 23BE58E0011; Thu, 24 Aug 2023 14:32:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 12A732800A4; Thu, 24 Aug 2023 14:32:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 0286B8E0011 for ; Thu, 24 Aug 2023 14:32:27 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id D83B8B21F2 for ; Thu, 24 Aug 2023 18:32:26 +0000 (UTC) X-FDA: 81159843492.17.38FD012 Received: from mail-ej1-f51.google.com (mail-ej1-f51.google.com [209.85.218.51]) by imf16.hostedemail.com (Postfix) with ESMTP id EED50180024 for ; Thu, 24 Aug 2023 18:32:24 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=VaTMowRb; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf16.hostedemail.com: domain of yosryahmed@google.com designates 209.85.218.51 as permitted sender) smtp.mailfrom=yosryahmed@google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1692901945; a=rsa-sha256; cv=none; b=Y+kuCKUxp2vJC+XCJhtW0gQvqF6r1oNve9HJ9HKe2on42+Ai0sV9+U407+vyet7SmKtSeP I45nObtqtWBaSugFh00ZX1xVgVNInCYrbCwYvKUF+ZM28eBJTUuQ7c9m7UkPsnmnBcRTee fPY1H7091mR3oKXwnhLz0q+DoaShwzg= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=VaTMowRb; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf16.hostedemail.com: domain of yosryahmed@google.com designates 209.85.218.51 as permitted sender) smtp.mailfrom=yosryahmed@google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1692901945; 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:in-reply-to:references:references:dkim-signature; bh=b+3+dmlDnZFrI+rLYEmzONKPLNzgiZ+Y0780EKixBdg=; b=ywEALGEy4n/pH/Pjka7mDg/zOPoG8IJvNhcnnhpj9ZtmtXvFT6RzrroWLCD48wXnxJ5mOc qEDeKsqfEXeNjGdQrhS243sghV+Re4QVFhFUmAYWIZ8MZfrmKM5+iJtEuJuMrigbZfFQ1A 4Sf0CPDUnLPlW82UTSwp7q8Nz1i3DP8= Received: by mail-ej1-f51.google.com with SMTP id a640c23a62f3a-9a18a4136a9so7662366b.2 for ; Thu, 24 Aug 2023 11:32:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1692901943; x=1693506743; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=b+3+dmlDnZFrI+rLYEmzONKPLNzgiZ+Y0780EKixBdg=; b=VaTMowRbb97l4wYN4V2TLMO0Cysl0+5fgFw3XRKsQ8NPtmJDHYaNb/FZgIWGmAScXP i0m3sP8CYsNfnkCAZqhTLk69U2t4Su8N7fzPHkjWzf6woVlnpYeTFJOdjbopcSU9YCiF K5VOjyT+0YB8As89VDM4WJ2PBnhiuDLKYnwTdtz1IUhQtKgNCsdNHwF2TmL6CKAbhRVe ELJuJ7gUgXTjR2og1xgmSII2lWgoERtPErGIHtCAqigeM9aQoBfeCqpKRZXLtV8M6NjX a0hkF1seKyj1+2BALJuILa8X+9xvHZMEWnp1iBRL2vVChfej1BhDCaLnelJCStbdNT/J saMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692901943; x=1693506743; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=b+3+dmlDnZFrI+rLYEmzONKPLNzgiZ+Y0780EKixBdg=; b=VnZJZAHnNNIDa5ae6ZrxZEo/PVZE9hOtUNG5/ePmHshfytC2WsWnGUojbWlWj/OLFl IczeiVHY0KfSmklFpnAmV3Wf7feAlQYgFCzsMEFpXOIw5gW9cobbMobqCNY+YItebDey R6vw9PA9SuSv+0Wa3r/IrlWnmRMOQTRrN/hErTYk1GroAQJenI2y+gEx81boEgvaLGJt 8GPedAVtoTH7SlMNdRAR8Hik+t+U1N19Z9+JNZ0VzYsQIxocDCXb3UtL/4Eyz7/hOV9H H0QlaAwPE3DOn1L1ACkSFp8ebYFOnaKW3y3fyXZw2jnMDW2Fwhxy7O0LmTbciYCtjFTd MWeg== X-Gm-Message-State: AOJu0YyCLwmKXYuj0e9S1AApRIt4+J9MEWY3obKXZ4gJt+Di76fSytJP p7ZeD39V+q25a92KFI30Zll8ziZqxLA+NFSaYlZbXQ== X-Google-Smtp-Source: AGHT+IHJSKeLVD6DR6KD6Xl66mDzcoycjKCoC9D3oU+ptw9RIDjabfvwWZJS27LYyh3lvexlD3+GdljBpK6hwuC4Fp4= X-Received: by 2002:a17:906:5307:b0:9a2:40e:2cad with SMTP id h7-20020a170906530700b009a2040e2cadmr2354481ejo.48.1692901943411; Thu, 24 Aug 2023 11:32:23 -0700 (PDT) MIME-Version: 1.0 References: <20230822024901.2412520-1-liushixin2@huawei.com> <87wmxk6d1m.fsf@yhuang6-desk2.ccr.corp.intel.com> In-Reply-To: <87wmxk6d1m.fsf@yhuang6-desk2.ccr.corp.intel.com> From: Yosry Ahmed Date: Thu, 24 Aug 2023 11:31:47 -0700 Message-ID: Subject: Re: [PATCH v2] mm: vmscan: reclaim anon pages if there are swapcache pages To: "Huang, Ying" Cc: Liu Shixin , Johannes Weiner , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , Andrew Morton , wangkefeng.wang@huawei.com, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: EED50180024 X-Stat-Signature: b7otf1hsbuh4ik8ohf8karwaqj3ceqqu X-HE-Tag: 1692901944-450889 X-HE-Meta: U2FsdGVkX18vXlGzjHiPY7PJRcZyPMA8fP4vmJa4NthVSPaX0R5BKp88R06iOIp0KeV1ihVqNoaY1u7rGcGUnWU9+DvGxqnJ7Dr5V/qE3NxXB3C8SoAoWj/VSn6Cb+R6vGPUUdajRTumzbk0JXRhJ/XND0h42dA9Zke6ZONxUu0Gh4QSbbpSPdgr5SY6gdJNpmrmMPA9bPdk8cG//qQeqnfxzxzDvq9xTWuoaSQvDKZPcBjaRbD6OGRi1q1jSwHSJ0V7Mr3JbcVtY8haLbOg/v2RYJYC0qlF968PqG9REdZBgX4YNwA6L95br7/NVRQF9Tl3HxcWrfOfn+QivEY1gD2LcB6a9AG+rSHjbyvHtMPvSkFIzsGOgWltUYaUzwqZnlqJhZGfnvgjWomigjwYKWuqhHjJzQWX4Ue+3smYewVLdgnV4CkuepyMEod8EK5yt3uICUa+8NM4Wd+RefAU/MqVyTwF3jDFN9GqhWjbfX2iGsOw64CnZuStpG2RDa0Vbo7Vy3vl50n1/piNXIGL+zu0Y8qf73pH2Gwgin18b/W3Q9iBQJfRDbUSSBHYOxckWJ+RMwxlk93b/FvsD/MKNqWJZ91xHWscJUb1eWhrW5jlj5UyiuJ3chEWTHPKg+m5eYrlPL9EiHb7ym1GPP4SXrEb/8EQsVwFnnDdVrj1atNLjOadjL84TOK6EWyC9jW9RkktieCJpn0T0nO+6cQI/OfEhCSVG/pAJ8yeQTlWSYXmzvMgjLj69OAg1UJ3P1NBa4tPRO6am1BFXUxo6QoZ5pqDYM4HqY+n+MP3DSGLfX/j/Y4Ifprj9VETMnC1lti2ZUQZmB3vCEpLA7gnj/rNO/Ur1Lnj+uaVUTKgUdqN0bff1rkvo9mpNdOouuYdmcJXnw5JTi1iagqKxtZTsi6+Qjhqqo8uyQhgDlYK4/LBZA8KA5Akc6dxxs4Y6/mDVpOkjGh7mkN5SGiCIo+ArAF iSO/zLMJ /nqGNMDSk7aJt9Q3xOBzGf8XGy0bwIRZdoHuLmVLn5UrYxiWVquXODjEuY1ov2zKqsPPxP9v93HM3Z8RB9UgAIcRxRMHv0gC9tzQlyELQz525eZuJW3YcnG9CTK0qLrSCwKPM1wRbNGQULN7Vn3oxxQpzjk9kGMyjQiubu8i/Ep4Wm3cviZ/mw7bCcmDf7nUcOMJsiIoLnmtoSfReodW+awEjxEPSgPxDp/2XuIzChdlHq6e9iNHVVZtd18VUOi0Wxy2umSuUqkKz/sL3j2IF8il4pFbB5ZFU6m8Zt71Tk0rp8nQ= 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: On Thu, Aug 24, 2023 at 1:51=E2=80=AFAM Huang, Ying = wrote: > > Yosry Ahmed writes: > > > On Mon, Aug 21, 2023 at 6:54=E2=80=AFPM Liu Shixin wrote: > >> > >> When spaces of swap devices are exhausted, only file pages can be recl= aimed. > >> But there are still some swapcache pages in anon lru list. This can le= ad > >> 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, w= hich > >> may not accurate but can solve the problem. > > > > Interesting find. I wonder if we really don't have any handling of > > this situation. > > > >> > >> 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_en= try_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 *memc= g); > >> 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(s= truct mem_cgroup *memcg) > >> return get_nr_swap_pages(); > >> } > >> > >> +static inline long mem_cgroup_get_nr_swapcache_pages(struct mem_cgrou= p *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_cg= roup *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); > >> +} > > > > Is there a reason why we cannot use NR_SWAPCACHE for cgroup v1? Isn't > > that being maintained regardless of cgroup version? It is not exposed > > in cgroup v1's memory.stat, but I don't think there is a reason we > > can't do that -- if only to document that it is being used with cgroup > > v1. > > > > > >> + > >> 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 =3D=3D 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 swapc= ache > >> + * pages can be freed? > >> + */ > >> + if (mem_cgroup_get_nr_swap_pages(memcg) + > >> + mem_cgroup_get_nr_swapcache_pages(memcg) > 0) > >> return true; > >> } > > > > I wonder if it would be more efficient to set a bit in struct > > scan_control if we only are out of swap spaces but have swap cache > > pages, and only isolate anon pages that are in the swap cache, instead > > of isolating random anon pages. We may end up isolating pages that are > > not in the swap cache for a few iterations and wasting cycles. > > Scanning swap cache directly will make the code more complex. IIUC, the > possibility for the swap device to be used up isn't high. If so, I > prefer the simpler implementation as that in this series. I did not mean that, sorry if I wasn't clear. I meant to set a bit in struct scan_control, and then in isolate_lru_folios() for anon lrus, we can skip isolating folios that are not in the swapcache if that bit is set. My main concern was that if we have a few pages in the swapcache we may end up wasting cycles scanning through a lot of other anonymous pages until we reach them. If that's too much complexity that's understandable. > > -- > Best Regards, > Huang, Ying