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 D9659C433F5 for ; Wed, 5 Oct 2022 02:12:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EDFED6B0072; Tue, 4 Oct 2022 22:12:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E8F6F6B0073; Tue, 4 Oct 2022 22:12:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D57868E0001; Tue, 4 Oct 2022 22:12:30 -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 B727E6B0072 for ; Tue, 4 Oct 2022 22:12:30 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 7F677AABD9 for ; Wed, 5 Oct 2022 02:12:30 +0000 (UTC) X-FDA: 79985271660.25.75ECA4C Received: from mail-wr1-f49.google.com (mail-wr1-f49.google.com [209.85.221.49]) by imf24.hostedemail.com (Postfix) with ESMTP id 1F16A180018 for ; Wed, 5 Oct 2022 02:12:29 +0000 (UTC) Received: by mail-wr1-f49.google.com with SMTP id bu30so3978159wrb.8 for ; Tue, 04 Oct 2022 19:12:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date; bh=SWVCoi1Gx8LnXulmNxgsJVtM05JVSDV+EFTdzhdAZAc=; b=Wlx6fTfIk9oWnVVFzqungmClkIN18LcCPyg1sz/G3iJSiZkgE/5aTMtFrbr6j3NC+a fjvmzhGf2irhlh/ldrW3MoRLz1ap0OuLCMZ1pKZ4rGq8rwk9leu6duN9ok5Ud3BbuHgx Ht5XUub6e36fvH6VJ2FRu2NSXu2I3bN0jUxPqCrVjt8vgj2hPgLfTxs1gzwmvwhmVlQd 1Bs1INnHS5DmvTNuuuoYiLyA8uvBTNvzRNcuU3GpYyqwG7aaM1ZNde2tNfigRYTprW7Y TiuKlB1/cXR+mvVrfvaMX8eNI0sPQmOrvchrlxPecMrUL0v0JXHpmODalnD1+Pcjb8up c9wQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date; bh=SWVCoi1Gx8LnXulmNxgsJVtM05JVSDV+EFTdzhdAZAc=; b=iOu0B4Cin46joEAoyMuZp4YUPKXEVrF+x2MkqFEMDHNgcxSXvbFSLnZjpOzw9jcDZw AkRdTR2jeVFy5WQC7Mhg5sqS2e5csNzUa/7w4LWSSI5rL6sY81Kw3ERbY81gXJiXkxZ8 d3qfPfLiHvOBQTyGQe/P0Jn2bxJZ8nV1HODvzoTqZxhe1dk7yLP/eHMRjQBN1BvfYnqh C5pC+L/qkIYDjb8AxZZ+zpfd6M0ETfTXqd2rm2Nkwp7qmCotYwSwPau3pD9FI1BZUtCu 4PdmnGTm+kQgs+kwoJlGL78OoIv/ASPZoEaNXl8vBJmia5ZjA4vkPuRPlOy/MMkKA5xo KfYg== X-Gm-Message-State: ACrzQf2Nh1om3QKRiGAUKf7T2FbYhHbWLCyG7A0InSawplXWQJlmDGxx 8wLG6fC38KZmd5TPX6NNi7elOV+ZFL+gnkNDhaeePo8XT2s= X-Google-Smtp-Source: AMsMyM49dRp2yqfHJSayRx3cgUWOmENXXmxTBsmZpwbH89qM5oE47pwoZYEVOZiPu7GziHPx90ZCyxaHNpRtO/VmYmU= X-Received: by 2002:a5d:6741:0:b0:22e:2c5c:d611 with SMTP id l1-20020a5d6741000000b0022e2c5cd611mr11956883wrw.210.1664935948647; Tue, 04 Oct 2022 19:12:28 -0700 (PDT) MIME-Version: 1.0 References: <20221004233446.787056-1-yosryahmed@google.com> In-Reply-To: <20221004233446.787056-1-yosryahmed@google.com> From: Yosry Ahmed Date: Tue, 4 Oct 2022 19:11:52 -0700 Message-ID: Subject: Re: [PATCH] mm/vmscan: check references from all memcgs for swapbacked memory To: Andrew Morton , Johannes Weiner , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song Cc: Greg Thelen , David Rientjes , Cgroups , Linux-MM Content-Type: text/plain; charset="UTF-8" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1664935950; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=SWVCoi1Gx8LnXulmNxgsJVtM05JVSDV+EFTdzhdAZAc=; b=xMhT040tTtnqwaejvpkgRVxlY3aiA5tf3vKG62jFloUaPwc7EvBCy/98l/JubKi+9Y+vIe NobTxc8fJstDXMcj6PhXquMPlFdzYVQtzXWko+GKUII/p2gCgc2IxvmfLSm2TnNTrKIvcI E5BCW8SMBloIy3zLSXbh5qrHU4sA7j4= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Wlx6fTfI; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf24.hostedemail.com: domain of yosryahmed@google.com designates 209.85.221.49 as permitted sender) smtp.mailfrom=yosryahmed@google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1664935950; a=rsa-sha256; cv=none; b=jEtzsC+SSJhsHOX70dtplwBbIZvzS55E7hNEOUesfQ0HmKn5jU35XSCaFgPRd+ONRsVGz8 MscjdyObfTeST+xfJJyyhHHbzlZ3l9FPqtYQwO8wk43MknO4c1GWpAC5P6TKwldFZzYC4y R30Hk467VZjarwhjIwjDrZqCE5h/nxc= X-Stat-Signature: 8y1drp33oswkwpkreyaan18kyq1ssouz X-Rspam-User: X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 1F16A180018 Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Wlx6fTfI; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf24.hostedemail.com: domain of yosryahmed@google.com designates 209.85.221.49 as permitted sender) smtp.mailfrom=yosryahmed@google.com X-HE-Tag: 1664935949-413406 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 Tue, Oct 4, 2022 at 4:34 PM Yosry Ahmed wrote: > > During page/folio reclaim, we check folio is referenced using > folio_referenced() to avoid reclaiming folios that have been recently > accessed (hot memory). The ratinale is that this memory is likely to be s/ratinale/rationale > accessed soon, and hence reclaiming it will cause a refault. > > For memcg reclaim, we pass in sc->target_mem_cgroup to > folio_referenced(), which means we only check accesses to the folio > from processes in the subtree of the target memcg. This behavior was > originally introduced by commit bed7161a519a ("Memory controller: make > page_referenced() cgroup aware") a long time ago. Back then, refaulted > pages would get charged to the memcg of the process that was faulting them > in. It made sense to only consider accesses coming from processes in the > subtree of target_mem_cgroup. If a page was charged to memcg A but only > being accessed by a sibling memcg B, we would reclaim it if memcg A is > under pressure. memcg B can then fault it back in and get charged for it > appropriately. > > Today, this behavior still makes sense for file pages. However, unlike > file pages, when swapbacked pages are refaulted they are charged to the > memcg that was originally charged for them during swapout. Which > means that if a swapbacked page is charged to memcg A but only used by > memcg B, and we reclaim it when memcg A is under pressure, it would > simply be faulted back in and charged again to memcg A once memcg B > accesses it. In that sense, accesses from all memcgs matter equally when > considering if a swapbacked page/folio is a viable reclaim target. > > Add folio_referenced_memcg() which decides what memcg we should pass to > folio_referenced() based on the folio type, and includes an elaborate > comment about why we should do so. This should help reclaim make better > decision and reduce refaults when reclaiming swapbacked memory that is s/decision/decisions > used by multiple memcgs. > > Signed-off-by: Yosry Ahmed > --- > mm/vmscan.c | 38 ++++++++++++++++++++++++++++++++++---- > 1 file changed, 34 insertions(+), 4 deletions(-) > > diff --git a/mm/vmscan.c b/mm/vmscan.c > index c5a4bff11da6..f9fa0f9287e5 100644 > --- a/mm/vmscan.c > +++ b/mm/vmscan.c > @@ -1443,14 +1443,43 @@ enum folio_references { > FOLIOREF_ACTIVATE, > }; > > +/* What memcg should we pass to folio_referenced()? */ > +static struct mem_cgroup *folio_referenced_memcg(struct folio *folio, > + struct mem_cgroup *target_memcg) > +{ > + /* > + * We check references to folios to make sure we don't reclaim hot > + * folios that are likely to be refaulted soon. We pass a memcg to > + * folio_referenced() to only check references coming from processes in > + * that memcg's subtree. > + * > + * For file folios, we only consider references from processes in the > + * subtree of the target memcg. If a folio is charged to > + * memcg A but is only referenced by processes in memcg B, we reclaim it > + * if memcg A is under pressure. If it is later accessed by memcg B it > + * will be faulted back in and charged to memcg B. For memcg A, this is > + * called memory that should be reclaimed. s/called/cold > + * > + * On the other hand, when swapbacked folios are faulted in, they get > + * charged to the memcg that was originally charged for them at the time > + * of swapping out. This means that if a folio that is charged to > + * memcg A gets swapped out, it will get charged back to A when *any* > + * memcg accesses it. In that sense, we need to consider references from > + * *all* processes when considering whether to reclaim a swapbacked > + * folio. > + */ > + return folio_test_swapbacked(folio) ? NULL : target_memcg; > +} > + > static enum folio_references folio_check_references(struct folio *folio, > struct scan_control *sc) > { > int referenced_ptes, referenced_folio; > unsigned long vm_flags; > + struct mem_cgroup *memcg = folio_referenced_memcg(folio, > + sc->target_mem_cgroup); > > - referenced_ptes = folio_referenced(folio, 1, sc->target_mem_cgroup, > - &vm_flags); > + referenced_ptes = folio_referenced(folio, 1, memcg, &vm_flags); > referenced_folio = folio_test_clear_referenced(folio); > > /* > @@ -2581,6 +2610,7 @@ static void shrink_active_list(unsigned long nr_to_scan, > > while (!list_empty(&l_hold)) { > struct folio *folio; > + struct mem_cgroup *memcg; > > cond_resched(); > folio = lru_to_folio(&l_hold); > @@ -2600,8 +2630,8 @@ static void shrink_active_list(unsigned long nr_to_scan, > } > > /* Referenced or rmap lock contention: rotate */ > - if (folio_referenced(folio, 0, sc->target_mem_cgroup, > - &vm_flags) != 0) { > + memcg = folio_referenced_memcg(folio, sc->target_mem_cgroup); > + if (folio_referenced(folio, 0, memcg, &vm_flags) != 0) { > /* > * Identify referenced, file-backed active folios and > * give them one more trip around the active list. So > -- > 2.38.0.rc1.362.ged0d419d3c-goog >