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 3F19EC433FE for ; Wed, 5 Oct 2022 14:04:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 504386B0072; Wed, 5 Oct 2022 10:04:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4B3FC6B0073; Wed, 5 Oct 2022 10:04:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 354A26B0074; Wed, 5 Oct 2022 10:04:11 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 2027B6B0072 for ; Wed, 5 Oct 2022 10:04:11 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 7B8EE80BA1 for ; Wed, 5 Oct 2022 14:04:10 +0000 (UTC) X-FDA: 79987065060.15.A757FED Received: from mail-qt1-f170.google.com (mail-qt1-f170.google.com [209.85.160.170]) by imf24.hostedemail.com (Postfix) with ESMTP id 552DF180018 for ; Wed, 5 Oct 2022 14:04:07 +0000 (UTC) Received: by mail-qt1-f170.google.com with SMTP id e20so9801624qts.1 for ; Wed, 05 Oct 2022 07:04:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpxchg-org.20210112.gappssmtp.com; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date; bh=FXi0XoAQOBYrGesfkJtJr1D/6fSwUi+ciVYcxjp3MmE=; b=mJHIld8cSRSCYaAW4MMM2N5yWGRlbk2nS1p0K7//H9ccXyb44BcbTd5JtB+mm91SHc vj4bKWYfab3HIaX1/tARDmcGIzlgnXluW22totZD2rYJHXsO53Ld0KGtv4vDjlHyvPgc 3h+0Z0GJG709ND7TPCiWA8Jl0OKqaVCJXUu5rLUK7hIrff6saZStgRN01tRn/upSvqRY ZLTSDrP0oEm9eagXypT89rzH+C0lLbywPuE/sIzA157xwrOa2+dK8MGvO2cNgJfGKkGy MIqr4F3cSF0X2KflYeEwEBag+4GX52eT00MimlhMUH8EdhC7N2gXHX8jIa2e/AqbJ7rb zDmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date; bh=FXi0XoAQOBYrGesfkJtJr1D/6fSwUi+ciVYcxjp3MmE=; b=IoS+FC9bOLzt+uMkmdTViIViiZ2bbgM2i0CvOdge2VVBrZkKzGhSF06feqnFd+Naaz 5mYhB82aVIxxwLrj1hYCk9Xyqv2NPgvgLicKd4Bciz+fgeAZwVMYXuKXUE8SVOurZyeI GGaxyiAsgGiupeFputTTScdX/hpp0ecWn8rbXfsgszgvfOtPgMMca1WKxz2yrP88o0Dx KDIWCx/Sa8SmJGxZuFEtoLqtYcOQl4Wcmzf4kFPQ1l08wyTW+/FjplQ5DE2TcvZxE+BV kjQoTdegk7uOVs7bBgXsk5kJYUZhqY+Fjz/dGn6HQ1HBh04rbX6D/b+ZP70ClB2VDwUR dNvg== X-Gm-Message-State: ACrzQf3zfiDXsGdb//6a2Gi5gZGUcrzxR1fwNiZfEHUwd4t+fnwSOucX UxX/ZzjgykaHywgwq9f72FPnCA== X-Google-Smtp-Source: AMsMyM7lJxCMUao8hqwiJsKbtmbmGv/p79yHVubsvJEB+tYhpAuTKmk+hxrJ0RZRC7ZS8teDJzoHFw== X-Received: by 2002:a05:622a:1111:b0:35c:b860:8468 with SMTP id e17-20020a05622a111100b0035cb8608468mr23640504qty.141.1664978646396; Wed, 05 Oct 2022 07:04:06 -0700 (PDT) Received: from localhost ([2620:10d:c091:480::282f]) by smtp.gmail.com with ESMTPSA id g12-20020a05620a40cc00b006ce3f1af120sm8634629qko.44.2022.10.05.07.04.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Oct 2022 07:04:05 -0700 (PDT) Date: Wed, 5 Oct 2022 10:04:05 -0400 From: Johannes Weiner To: Yosry Ahmed Cc: Andrew Morton , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , Greg Thelen , David Rientjes , cgroups@vger.kernel.org, linux-mm@kvack.org Subject: Re: [PATCH] mm/vmscan: check references from all memcgs for swapbacked memory Message-ID: References: <20221004233446.787056-1-yosryahmed@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20221004233446.787056-1-yosryahmed@google.com> ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1664978647; 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=FXi0XoAQOBYrGesfkJtJr1D/6fSwUi+ciVYcxjp3MmE=; b=ktQSyGLGADR4ccartZNtvWVQZ/8xbsTNcqGpF2IF2PzwMWVqx0VeOw3MaV8/h9HOPyMF6i zcCyD5blSs22IB9mqg78Mt53xggj5dHmjfA9mLBI6qrNawybcuIEqmzUwnjb1TTNTZ7Dbf XPvtkX4Mle9LBYdMF5DjNdO2/dOmgTs= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=cmpxchg-org.20210112.gappssmtp.com header.s=20210112 header.b=mJHIld8c; spf=pass (imf24.hostedemail.com: domain of hannes@cmpxchg.org designates 209.85.160.170 as permitted sender) smtp.mailfrom=hannes@cmpxchg.org; dmarc=pass (policy=none) header.from=cmpxchg.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1664978647; a=rsa-sha256; cv=none; b=2yH7WXybFsunPln6kPg1mv1xE01mJIWMaU77H0gBdcYm9gY/gyeIqcaIz3dQOJYqROzuuA QcYxe+MQUOKrVZ5YJ/AtYEzeKmLSqK4CrCdLRNKVgh+x89aVvnh8C/40zEu+Sq+T4Q/sfh W3J/tUOLN76CgSHMQAc9NFvpYSVr7w0= X-Rspam-User: Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=cmpxchg-org.20210112.gappssmtp.com header.s=20210112 header.b=mJHIld8c; spf=pass (imf24.hostedemail.com: domain of hannes@cmpxchg.org designates 209.85.160.170 as permitted sender) smtp.mailfrom=hannes@cmpxchg.org; dmarc=pass (policy=none) header.from=cmpxchg.org X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 552DF180018 X-Stat-Signature: rk5xaez5k19xtzih5dtnpkubo65um6f9 X-HE-Tag: 1664978647-245051 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: Hi Yosry, On Tue, Oct 04, 2022 at 11:34:46PM +0000, 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 > 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 > used by multiple memcgs. Great observation, and I agree with this change. Just one nitpick: > 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. > + * > + * 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) { Would you mind moving this to folio_referenced() directly? There is already a comment and branch in there that IMO would extend quite naturally to cover the new exception: /* * If we are reclaiming on behalf of a cgroup, skip * counting on behalf of references from different * cgroups */ if (memcg) { rwc.invalid_vma = invalid_folio_referenced_vma; } That would keep the decision-making and doc in one place. Thanks! Johannnes