Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp6325099rwb; Mon, 5 Dec 2022 10:39:50 -0800 (PST) X-Google-Smtp-Source: AA0mqf7cxKALqllUC1aAkMVDoaaC39Jo6BEY1cC+7TfD+JuM/7nSKKPjFCc5vAP8Vzvi+wcZ9ZUe X-Received: by 2002:a17:902:f7ca:b0:189:a884:1ea0 with SMTP id h10-20020a170902f7ca00b00189a8841ea0mr26828827plw.92.1670265590044; Mon, 05 Dec 2022 10:39:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670265590; cv=none; d=google.com; s=arc-20160816; b=yZzvlx5KvhLIqQ/GMMx/ULnIiQt7Lv1+knwbBp4wB6zWHOEL+5tRe8e2G3gdozvBhG AY5uj2vkFAhUgCO3yJ1mnM3T/EHn6/CazNEq89EmnlrBGH7gmcNopUZFS4cnDK3WI+Mj VKJjrUxUMwVdms/rLnopVp1njMG70C7/IJO9g4HYyQ5IQFQC3+No/8GfjyVH90/j+kJq 4LBHrX4pLSZpPne9A2EyPR7KsAcqrxztq+GXYQWS/Cp4p/75ahvwylEj3phLevVpuRHH 8oZsQ/5adFGrhsO5VPGg9NLvKRys65ZuiyFbmCSDaa1nwygifAIHFRfV38IQ69fDyFed 9EMA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=DgolFyTl0+E6DqEY3qmBWlaeGZA7Win71lyPPhahR4w=; b=WZhnmaHsjjtJUi9i+B/jOjt3RiwrRwHv7bnPwCj13mCYj501wOUMCwY9d8YW/4VnJC TReOJuuHeN8OR6jnNeayMQjzpQX6S3XovvY9OO3AksYWNFiQUxYYdKYmeN9P8mb7gSdx Mu/gdkLjew9tE/1m0GmYdKD3PsVBtXURzNE+62CLQPfdNUVI9j4CftDDTGaaBY7z2pXW 8+j8J3DwvifWL8nZOuPnDUE+kifwr6zwhmCv8+ElP0TzxK2PyZxdTrigHYT2p7JvuWFn fK5T5LsuM3OKvqUhkDZGY51Z6r4HzR9gkNiwuRczTG8NCveoK5pAuD/4uK3zWfWl7Idh QjsA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=JLjQdVHY; 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=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k3-20020aa788c3000000b00574fcd99824si16890050pff.73.2022.12.05.10.39.35; Mon, 05 Dec 2022 10:39:50 -0800 (PST) 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; dkim=pass header.i=@gmail.com header.s=20210112 header.b=JLjQdVHY; 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=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232766AbiLERwk (ORCPT + 81 others); Mon, 5 Dec 2022 12:52:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44380 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232564AbiLERvs (ORCPT ); Mon, 5 Dec 2022 12:51:48 -0500 Received: from mail-pg1-x536.google.com (mail-pg1-x536.google.com [IPv6:2607:f8b0:4864:20::536]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 777DC1EC5A for ; Mon, 5 Dec 2022 09:51:46 -0800 (PST) Received: by mail-pg1-x536.google.com with SMTP id 136so11148843pga.1 for ; Mon, 05 Dec 2022 09:51:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=DgolFyTl0+E6DqEY3qmBWlaeGZA7Win71lyPPhahR4w=; b=JLjQdVHYnbMLvg/MZf1ZTWToimKDYwAtFuiUWucB+fDvYlxKAHErVa5KYmlmM/EpS9 RT/5relmWtbdIgrAGRhfpKeiQUyaR7jXbSnX5+BRW0HmE2pU87PcGCHhAXOgHrWvYHdF GdfV2SAlbQU3XYAfWbYomKa66IMWw7xbSzCy43uGlFRUq4hOjkRDZrqf5HngpeEpY7Xz U2Pn9HA6G1iZNDSRUMyRC5bIAJKR1ocXNoR2Ipl9eRpK+V+2eBV+jvbBRYLZMe3IeqVs dD34GhmVEPv55SFLfTRLTcJ31Zpq65QWrBTB5OAT1z1/FHEbvfqgr48fVzmTWHwjph3g n4ew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=DgolFyTl0+E6DqEY3qmBWlaeGZA7Win71lyPPhahR4w=; b=DFNRsWBDIoQf+cuJSND36eqrECHXK25KdlGYgzRHxVoWgha9HGkF+zlT/fhU9dB9Rs BInGSCasHs62tRZhCJ8mC4tp0TqbCsovitFZtxbd23qNbXINMm+zToHOJ60qOxSzRoD4 Q80aCT5g3XoR/Visq1WaHvOBCOLj1E4BlrKJgqDqqZvqZxG7CqxaKb3PZlm3BAhyXW+B sKYJyjiavvw8g6Pnj55N+WKYaG+1ond07s7jA8HF5kR4ZrvE5DuFBW4OzMkH7gNR75Xd GiDUTZ6Q9ZkaSiC9RchxLCopoT4QNdHZ6X+vOYkctv/SVhKfPmsBXUHenWsdiHNdqg5J UKog== X-Gm-Message-State: ANoB5pmVgICGvXgk6hKXDMzfDuABbX5h1Ke4bCI2PhIHZTwKlWid7MLF zmSI8aMpcDJRxEsLwnoVgKs= X-Received: by 2002:a05:6a00:4307:b0:576:ba28:29a8 with SMTP id cb7-20020a056a00430700b00576ba2829a8mr9923939pfb.47.1670262705906; Mon, 05 Dec 2022 09:51:45 -0800 (PST) Received: from localhost (fwdproxy-prn-019.fbsv.net. [2a03:2880:ff:13::face:b00c]) by smtp.gmail.com with ESMTPSA id b7-20020a170902650700b00186b549cdc2sm10904824plk.157.2022.12.05.09.51.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Dec 2022 09:51:45 -0800 (PST) From: Nhat Pham To: akpm@linux-foundation.org Cc: hannes@cmpxchg.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, bfoster@redhat.com, willy@infradead.org, kernel-team@meta.com Subject: [PATCH v2 2/4] workingset: refactor LRU refault to expose refault recency check Date: Mon, 5 Dec 2022 09:51:38 -0800 Message-Id: <20221205175140.1543229-3-nphamcs@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20221205175140.1543229-1-nphamcs@gmail.com> References: <20221205175140.1543229-1-nphamcs@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,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 In preparation for computing recently evicted pages in cachestat, refactor workingset_refault and lru_gen_refault to expose a helper function that would test if an evicted page is recently evicted. Signed-off-by: Nhat Pham --- include/linux/swap.h | 1 + mm/workingset.c | 143 +++++++++++++++++++++++++++++-------------- 2 files changed, 99 insertions(+), 45 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index a18cf4b7c724..dae6f6f955eb 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -361,6 +361,7 @@ static inline void folio_set_swap_entry(struct folio *folio, swp_entry_t entry) } /* linux/mm/workingset.c */ +bool workingset_test_recent(void *shadow, bool file, bool *workingset); void workingset_age_nonresident(struct lruvec *lruvec, unsigned long nr_pages); void *workingset_eviction(struct folio *folio, struct mem_cgroup *target_memcg); void workingset_refault(struct folio *folio, void *shadow); diff --git a/mm/workingset.c b/mm/workingset.c index 79585d55c45d..44b331ce3040 100644 --- a/mm/workingset.c +++ b/mm/workingset.c @@ -244,6 +244,30 @@ static void *lru_gen_eviction(struct folio *folio) return pack_shadow(mem_cgroup_id(memcg), pgdat, token, refs); } +/* + * Test if the folio is recently evicted. + * + * As a side effect, also populates the references with + * values unpacked from the shadow of the evicted folio. + */ +static bool lru_gen_test_recent(void *shadow, bool file, int *memcgid, + struct pglist_data **pgdat, unsigned long *token, bool *workingset) +{ + struct mem_cgroup *eviction_memcg; + struct lruvec *lruvec; + struct lru_gen_struct *lrugen; + unsigned long min_seq; + + unpack_shadow(shadow, memcgid, pgdat, token, workingset); + eviction_memcg = mem_cgroup_from_id(*memcgid); + + lruvec = mem_cgroup_lruvec(eviction_memcg, *pgdat); + lrugen = &lruvec->lrugen; + + min_seq = READ_ONCE(lrugen->min_seq[file]); + return !((*token >> LRU_REFS_WIDTH) != (min_seq & (EVICTION_MASK >> LRU_REFS_WIDTH))); +} + static void lru_gen_refault(struct folio *folio, void *shadow) { int hist, tier, refs; @@ -258,23 +282,24 @@ static void lru_gen_refault(struct folio *folio, void *shadow) int type = folio_is_file_lru(folio); int delta = folio_nr_pages(folio); - unpack_shadow(shadow, &memcg_id, &pgdat, &token, &workingset); - - if (pgdat != folio_pgdat(folio)) - return; rcu_read_lock(); memcg = folio_memcg_rcu(folio); + + if (!lru_gen_test_recent(shadow, type, &memcg_id, &pgdat, &token, + &workingset)) + goto unlock; + if (memcg_id != mem_cgroup_id(memcg)) goto unlock; + if (pgdat != folio_pgdat(folio)) + goto unlock; + lruvec = mem_cgroup_lruvec(memcg, pgdat); lrugen = &lruvec->lrugen; - min_seq = READ_ONCE(lrugen->min_seq[type]); - if ((token >> LRU_REFS_WIDTH) != (min_seq & (EVICTION_MASK >> LRU_REFS_WIDTH))) - goto unlock; hist = lru_hist_from_seq(min_seq); /* see the comment in folio_lru_refs() */ @@ -306,6 +331,12 @@ static void *lru_gen_eviction(struct folio *folio) return NULL; } +static bool lru_gen_test_recent(void *shadow, int type, int *memcgid, + struct pglist_data **pgdat, unsigned long *token, bool *workingset) +{ + return true; +} + static void lru_gen_refault(struct folio *folio, void *shadow) { } @@ -373,40 +404,32 @@ void *workingset_eviction(struct folio *folio, struct mem_cgroup *target_memcg) folio_test_workingset(folio)); } -/** - * workingset_refault - Evaluate the refault of a previously evicted folio. - * @folio: The freshly allocated replacement folio. - * @shadow: Shadow entry of the evicted folio. +/* + * Test if the folio is recently evicted by checking if + * refault distance of shadow exceeds workingset size. * - * Calculates and evaluates the refault distance of the previously - * evicted folio in the context of the node and the memcg whose memory - * pressure caused the eviction. + * As a side effect, populate workingset with the value + * unpacked from shadow. */ -void workingset_refault(struct folio *folio, void *shadow) +bool workingset_test_recent(void *shadow, bool file, bool *workingset) { - bool file = folio_is_file_lru(folio); struct mem_cgroup *eviction_memcg; struct lruvec *eviction_lruvec; unsigned long refault_distance; unsigned long workingset_size; - struct pglist_data *pgdat; - struct mem_cgroup *memcg; - unsigned long eviction; - struct lruvec *lruvec; unsigned long refault; - bool workingset; + int memcgid; - long nr; + struct pglist_data *pgdat; + unsigned long eviction; - if (lru_gen_enabled()) { - lru_gen_refault(folio, shadow); - return; - } + if (lru_gen_enabled()) + return lru_gen_test_recent(shadow, file, &memcgid, + &pgdat, &eviction, workingset); - unpack_shadow(shadow, &memcgid, &pgdat, &eviction, &workingset); + unpack_shadow(shadow, &memcgid, &pgdat, &eviction, workingset); eviction <<= bucket_order; - rcu_read_lock(); /* * Look up the memcg associated with the stored ID. It might * have been deleted since the folio's eviction. @@ -425,7 +448,8 @@ void workingset_refault(struct folio *folio, void *shadow) */ eviction_memcg = mem_cgroup_from_id(memcgid); if (!mem_cgroup_disabled() && !eviction_memcg) - goto out; + return false; + eviction_lruvec = mem_cgroup_lruvec(eviction_memcg, pgdat); refault = atomic_long_read(&eviction_lruvec->nonresident_age); @@ -447,21 +471,6 @@ void workingset_refault(struct folio *folio, void *shadow) */ refault_distance = (refault - eviction) & EVICTION_MASK; - /* - * The activation decision for this folio is made at the level - * where the eviction occurred, as that is where the LRU order - * during folio reclaim is being determined. - * - * However, the cgroup that will own the folio is the one that - * is actually experiencing the refault event. - */ - nr = folio_nr_pages(folio); - memcg = folio_memcg(folio); - pgdat = folio_pgdat(folio); - lruvec = mem_cgroup_lruvec(memcg, pgdat); - - mod_lruvec_state(lruvec, WORKINGSET_REFAULT_BASE + file, nr); - mem_cgroup_flush_stats_delayed(); /* * Compare the distance to the existing workingset size. We @@ -483,8 +492,51 @@ void workingset_refault(struct folio *folio, void *shadow) NR_INACTIVE_ANON); } } - if (refault_distance > workingset_size) + + return refault_distance <= workingset_size; +} + +/** + * workingset_refault - Evaluate the refault of a previously evicted folio. + * @folio: The freshly allocated replacement folio. + * @shadow: Shadow entry of the evicted folio. + * + * Calculates and evaluates the refault distance of the previously + * evicted folio in the context of the node and the memcg whose memory + * pressure caused the eviction. + */ +void workingset_refault(struct folio *folio, void *shadow) +{ + bool file = folio_is_file_lru(folio); + struct pglist_data *pgdat; + struct mem_cgroup *memcg; + struct lruvec *lruvec; + bool workingset; + long nr; + + if (lru_gen_enabled()) { + lru_gen_refault(folio, shadow); + return; + } + + rcu_read_lock(); + + nr = folio_nr_pages(folio); + memcg = folio_memcg(folio); + pgdat = folio_pgdat(folio); + lruvec = mem_cgroup_lruvec(memcg, pgdat); + + if (!workingset_test_recent(shadow, file, &workingset)) { + /* + * The activation decision for this folio is made at the level + * where the eviction occurred, as that is where the LRU order + * during folio reclaim is being determined. + * + * However, the cgroup that will own the folio is the one that + * is actually experiencing the refault event. + */ goto out; + } folio_set_active(folio); workingset_age_nonresident(lruvec, nr); @@ -498,6 +550,7 @@ void workingset_refault(struct folio *folio, void *shadow) mod_lruvec_state(lruvec, WORKINGSET_RESTORE_BASE + file, nr); } out: + mod_lruvec_state(lruvec, WORKINGSET_REFAULT_BASE + file, nr); rcu_read_unlock(); } -- 2.30.2