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 A3398C4332F for ; Sun, 4 Dec 2022 09:34:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3DBBE8E0002; Sun, 4 Dec 2022 04:34:34 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 38ACD8E0001; Sun, 4 Dec 2022 04:34:34 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 22B178E0002; Sun, 4 Dec 2022 04:34:34 -0500 (EST) 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 12A948E0001 for ; Sun, 4 Dec 2022 04:34:34 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id D813D40AAF for ; Sun, 4 Dec 2022 09:34:33 +0000 (UTC) X-FDA: 80204113626.09.52786D4 Received: from mail-vs1-f43.google.com (mail-vs1-f43.google.com [209.85.217.43]) by imf07.hostedemail.com (Postfix) with ESMTP id 7F56240012 for ; Sun, 4 Dec 2022 09:34:33 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=AbRL9P6Q; spf=pass (imf07.hostedemail.com: domain of almasrymina@google.com designates 209.85.217.43 as permitted sender) smtp.mailfrom=almasrymina@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1670146473; 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=WDZCPeHF38SRI2PhowrQk3SQC9N/a7ammpRRQqsEwNs=; b=J52/nuFqF9J7GtM7+xkGkLWGljFz913+zg1uwvBDD0rfGIM9qco8BUn7VkAazyJNusEwcW WuvQIGa/JGXPizJDsw6RrtSwlIEaZzEfrY2ru8dbA6r1L95Wt0A0+G0l619oyImcfXca1S 7wD1QVsUIq51rotZmpXzzMwtyOIGpXc= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=AbRL9P6Q; spf=pass (imf07.hostedemail.com: domain of almasrymina@google.com designates 209.85.217.43 as permitted sender) smtp.mailfrom=almasrymina@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1670146473; a=rsa-sha256; cv=none; b=ysNwI79ygR5ucE3TkuvMBxrnUZwEE8w6Lmv6y48PnDx4wE4zX8pDmn78jn0FptWXiE5UbY OUg1WGZn+S/U8mKwPrOZZsCTp/BmloEUqvY3MpcaaJQPn/JENkai1+QRWnVyxXeX2JT2aa PZuD+ApIcIAkaP0moaV4qzPmdF901JA= Received: by mail-vs1-f43.google.com with SMTP id m4so8536320vsc.6 for ; Sun, 04 Dec 2022 01:34:33 -0800 (PST) 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:message-id:reply-to; bh=WDZCPeHF38SRI2PhowrQk3SQC9N/a7ammpRRQqsEwNs=; b=AbRL9P6Qn3/xgQIx/pr3LG8cbkJVDfT9OVAUbK6KrzlboQqDcc5qDyhnDPrkt6l479 6rRzZRyqU1Zjr9waeEtgH95w2xviLHF3x7b/3+9n7GZJkghEhGItfl4JBU2S3yMof1PM l9KrRyak1Z5nTAZsL+zcloajEUiDOTf8EJm5qoPrmDDyKJF1iwwojBjKm0wxkTy/aG3M Y2PpV2hwM/46KrZUUTf9iLgA6DcgX7p8dwjTIEI3v8H1W++FnQOVGWQqKNXvBO8lraGt +awKel+zwyFLh6+hxXcmHIMCL0DfwS+eqD46uh25JWEaEhjR3kdZLRDep073ioU5+Fys a5/A== 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:message-id :reply-to; bh=WDZCPeHF38SRI2PhowrQk3SQC9N/a7ammpRRQqsEwNs=; b=Lg72rLBuGOBZJFHuUm0kt31lf2O4Hp3y7pfJFfLCanJmxKBGJH2OgKDToQwKIbq4lG j15vlEw8Rgw/kunOSGNlllRG88ogbV3qxFQkBC/dPcP5m7ea8kRkwnMtdWGRfJG6jfew pwdArNqV2InLeAk3S3AiKCAuYqyTL89au3DiuKyL9j40U3XOEVARbpQOcAOw8a/AzmPN 4EiTqQFiEiHorEp2Ksxx0SdlPxJE/SG1W3NsSE3/OjrSGaILMaxLhmqZ1gvm0vkOLMET fLEy/mXLd5A5A/jcCzYt3p61pHfgfN1Mp6CoKe2OCb472WD7wKGhMwr7rukmrVq0MZjE cB/g== X-Gm-Message-State: ANoB5pmgf2m8w/nD6U/E0DQljOwCkM2oI5C2NcAlFhejnMJ4Jkw8BRJu Xyls5L1/TQiWGwXbdiZ75au6gF27KpRXabORvzkKgg== X-Google-Smtp-Source: AA0mqf6odUcczZRCsSLrEvyEneoIneIZ0eqanYuxkKWHUX3keeCnDUzxCsHAEQ+wk1zEFWUiilitWrSHk8Yh/i5MNC0= X-Received: by 2002:a67:ea04:0:b0:3a7:d7bc:c2e9 with SMTP id g4-20020a67ea04000000b003a7d7bcc2e9mr34388799vso.61.1670146472628; Sun, 04 Dec 2022 01:34:32 -0800 (PST) MIME-Version: 1.0 References: <20221203011120.2361610-1-almasrymina@google.com> In-Reply-To: From: Mina Almasry Date: Sun, 4 Dec 2022 01:34:20 -0800 Message-ID: Subject: Re: [PATCH v1] [mm-unstable] mm: Fix memcg reclaim on memory tiered systems To: Wei Xu Cc: Andrew Morton , Johannes Weiner , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , Huang Ying , Yang Shi , Yosry Ahmed , fvdl@google.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Stat-Signature: xha43sqjqsrou1rn6yoqc6hnb68nypgw X-Rspam-User: X-Spamd-Result: default: False [-2.90 / 9.00]; BAYES_HAM(-6.00)[100.00%]; SORBS_IRL_BL(3.00)[209.85.217.43:from]; BAD_REP_POLICIES(0.10)[]; RCVD_NO_TLS_LAST(0.10)[]; MIME_GOOD(-0.10)[text/plain]; MIME_TRACE(0.00)[0:+]; RCVD_COUNT_TWO(0.00)[2]; FROM_EQ_ENVFROM(0.00)[]; DMARC_POLICY_ALLOW(0.00)[google.com,reject]; RCPT_COUNT_TWELVE(0.00)[13]; DKIM_TRACE(0.00)[google.com:+]; TO_MATCH_ENVRCPT_SOME(0.00)[]; PREVIOUSLY_DELIVERED(0.00)[linux-mm@kvack.org]; R_DKIM_ALLOW(0.00)[google.com:s=20210112]; ARC_SIGNED(0.00)[hostedemail.com:s=arc-20220608:i=1]; FROM_HAS_DN(0.00)[]; R_SPF_ALLOW(0.00)[+ip4:209.85.128.0/17]; TO_DN_SOME(0.00)[]; ARC_NA(0.00)[] X-Rspamd-Queue-Id: 7F56240012 X-Rspamd-Server: rspam06 X-HE-Tag: 1670146473-266721 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 Fri, Dec 2, 2022 at 8:14 PM Wei Xu wrote: > > On Fri, Dec 2, 2022 at 5:11 PM Mina Almasry wrote: > > > > commit 3f1509c57b1b ("Revert "mm/vmscan: never demote for memcg > > reclaim"") enabled demotion in memcg reclaim, which is the right thing > > to do, however, I suspect it introduced a regression in the behavior of > > try_to_free_mem_cgroup_pages(). > > > > The callers of try_to_free_mem_cgroup_pages() expect it to attempt to > > reclaim - not demote - nr_pages from the cgroup. I.e. the memory usage > > of the cgroup should reduce by nr_pages. The callers expect > > try_to_free_mem_cgroup_pages() to also return the number of pages > > reclaimed, not demoted. > > > > However, what try_to_free_mem_cgroup_pages() actually does is it > > unconditionally counts demoted pages as reclaimed pages. So in practice > > when it is called it will often demote nr_pages and return the number of > > demoted pages to the caller. Demoted pages don't lower the memcg usage, > > and so I think try_to_free_mem_cgroup_pages() is not actually doing what > > the callers want it to do. > > > > I suspect various things work suboptimally on memory systems or don't > > work at all due to this: > > > > - memory.high enforcement likely doesn't work (it just demotes nr_pages > > instead of lowering the memcg usage by nr_pages). > > - try_charge_memcg() will keep retrying the charge while > > try_to_free_mem_cgroup_pages() is just demoting pages and not actually > > making any room for the charge. > > - memory.reclaim has a wonky interface. It advertises to the user it > > reclaims the provided amount but it will actually demote that amount. > > > > There may be more effects to this issue. > > > > To fix these issues I propose shrink_folio_list() to only count pages > > demoted from inside of sc->nodemask to outside of sc->nodemask as > > 'reclaimed'. > > > > For callers such as reclaim_high() or try_charge_memcg() that set > > sc->nodemask to NULL, try_to_free_mem_cgroup_pages() will try to > > actually reclaim nr_pages and return the number of pages reclaimed. No > > demoted pages would count towards the nr_pages requirement. > > > > For callers such as memory_reclaim() that set sc->nodemask, > > try_to_free_mem_cgroup_pages() will free nr_pages from that nodemask > > with either reclaim or demotion. > > > > Tested this change using memory.reclaim interface. With this change, > > > > echo "1m" > memory.reclaim > > > > Will cause freeing of 1m of memory from the cgroup regardless of the > > demotions happening inside. > > > > echo "1m nodes=0" > memory.reclaim > > > > Will cause freeing of 1m of node 0 by demotion if a demotion target is > > available, and by reclaim if no demotion target is available. > > > > Signed-off-by: Mina Almasry > > > > --- > > > > This is developed on top of mm-unstable largely because I need the > > memory.reclaim nodes= arg to test it properly. > > --- > > mm/vmscan.c | 13 ++++++++++++- > > 1 file changed, 12 insertions(+), 1 deletion(-) > > > > diff --git a/mm/vmscan.c b/mm/vmscan.c > > index 2b42ac9ad755..8f6e993b870d 100644 > > --- a/mm/vmscan.c > > +++ b/mm/vmscan.c > > @@ -1653,6 +1653,7 @@ static unsigned int shrink_folio_list(struct list_head *folio_list, > > LIST_HEAD(free_folios); > > LIST_HEAD(demote_folios); > > unsigned int nr_reclaimed = 0; > > + unsigned int nr_demoted = 0; > > unsigned int pgactivate = 0; > > bool do_demote_pass; > > struct swap_iocb *plug = NULL; > > @@ -2085,7 +2086,17 @@ static unsigned int shrink_folio_list(struct list_head *folio_list, > > /* 'folio_list' is always empty here */ > > > > /* Migrate folios selected for demotion */ > > - nr_reclaimed += demote_folio_list(&demote_folios, pgdat); > > + nr_demoted = demote_folio_list(&demote_folios, pgdat); > > + > > + /* > > + * Only count demoted folios as reclaimed if we demoted them from > > + * inside of the nodemask to outside of the nodemask, hence reclaiming > > + * pages in the nodemask. > > + */ > > + if (sc->nodemask && node_isset(pgdat->node_id, *sc->nodemask) && > > + !node_isset(next_demotion_node(pgdat->node_id), *sc->nodemask)) > > next_demotion_node() is just the first demotion target node. Demotion > can fall back to other allowed target nodes returned by > node_get_allowed_targets(). When the page is demoted to a fallback > node and this fallback node is in sc->nodemask, nr_demoted should not > be added into nr_reclaimed, either. > Thanks for reviewing Wei, I did indeed miss this. > One way to address this issue is to pass sc->nodemask into > demote_folio_list() and exclude sc->nodemask from the allowed target > demotion nodes. > This makes sense to me. Applied this change and uploaded v2: https://lore.kernel.org/linux-mm/20221204093008.2620459-1-almasrymina@google.com/T/#u > > + nr_reclaimed += nr_demoted; > > + > > /* Folios that could not be demoted are still in @demote_folios */ > > if (!list_empty(&demote_folios)) { > > /* Folios which weren't demoted go back on @folio_list */ > > -- > > 2.39.0.rc0.267.gcb52ba06e7-goog