From patchwork Sun Feb 19 07:33:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nhat Pham X-Patchwork-Id: 59088 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp756084wrn; Sun, 19 Feb 2023 00:39:48 -0800 (PST) X-Google-Smtp-Source: AK7set+urF/qPMM4CW2+80mP2UvLjTck+wtNAQXkkmqnhXe4Uy54mVLr+xSJNdS1s2ifKherTc5N X-Received: by 2002:a05:6a00:1bc8:b0:5a8:473e:2fdc with SMTP id o8-20020a056a001bc800b005a8473e2fdcmr2317756pfw.12.1676795987932; Sun, 19 Feb 2023 00:39:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676795987; cv=none; d=google.com; s=arc-20160816; b=pfGfZbZgIbvhjzNGurW8KB3J4ZBPZfmi+jNyD5gBV05cRriFumJFS8Et/I17efS3vi 4u+BMrU3kkuYjnqPuFdl/b9lRJThyXtg/5GKGG2VfLHacFJ3So8b62ki4te3npQsLy8l q0cSVPYDRz1NbqZKA2qmwc+ccbTK1v1cyyjGiSzoSkFt7vFavl4fdUGHixov6s42F5o7 oE7R4sTtYZYYMebR378Ryb9dMfs5IKV2DbhI/utKtllC+ynqb01gZoSOdLN5rw2kctm6 g1musEu3SYh0XDxUb1umIeR1buPR7FmLM6JfMThWX4EhuAipuoueQY6aiGbSdim8Nfpb OW5Q== 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=foTbT4QwdeCG8p/+pG7+6hYxA8iHmzjKmefWFVB0gjc=; b=HGUKlsrHLIpQyFUpAKk/E+cwQVrR0kjtFOHO0lXLCQIuvx8ae68kXGqzo37IR/TcbU 1ZSDzh3nQYG5gE6IZMmvUosEdLyVpnnkcVBPVp7bTUqGgBlNIIPGtjLbBPStkrn3x6Zf gly7PdGnvyr2nMlvHvCErJ6YQseIjwBWfFwJZtz+5DxKWoNHfVV3cOA29YjvuCjPVcrK 1968zne3BrUnrVnMpAj5aqleeVoBxes2hfJ2dWzIMfbg9AuLi0hWicsYXuIHGhTjLmyf 5IPQfIp6KB9vynhLlQTrja1165aXl7JNB6DpOFzJkcM/WR2ttoquuK5VV9zMEkj51y4/ H/NA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=M942xidG; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j187-20020a62c5c4000000b005731d316830si10287048pfg.23.2023.02.19.00.39.34; Sun, 19 Feb 2023 00:39:47 -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=M942xidG; 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 S229709AbjBSHeI (ORCPT + 99 others); Sun, 19 Feb 2023 02:34:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41312 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229656AbjBSHeB (ORCPT ); Sun, 19 Feb 2023 02:34:01 -0500 Received: from mail-oa1-x29.google.com (mail-oa1-x29.google.com [IPv6:2001:4860:4864:20::29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0FC1AFF33; Sat, 18 Feb 2023 23:33:33 -0800 (PST) Received: by mail-oa1-x29.google.com with SMTP id 586e51a60fabf-171d6fa4c5dso317737fac.6; Sat, 18 Feb 2023 23:33:33 -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=foTbT4QwdeCG8p/+pG7+6hYxA8iHmzjKmefWFVB0gjc=; b=M942xidGgMKr/OauZOPmRqRj/n2NxWP9yZV+FKV0A0eh+VjgQBEhFdESqBfA0GlCuc lRqkkkX8S8t6DWDnX/Bt5qBtZglDeLAwAiO5VrB9tzIsYBi7MmITfMxahbeV9o20Ui// 21WZtokjKghT2CQKgpfQ8cxaj9PRTsbnj6o0m7YaxXlDmx3DDOtfJEdaDfKtgxy5r1uO SrS3Rofj0+iN93Q9Jwts+h98i5F0HPQVpOnn2pYXG5oiuIHypLxc1wGgvbot9Hnmuoid /YX+UV0H+dP4Mj+E9GHdI8k8hEQmmZglej7TjZHdQ1FrDRPjl6eHgcKt2Ad0i0D7pU7l rX1A== 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=foTbT4QwdeCG8p/+pG7+6hYxA8iHmzjKmefWFVB0gjc=; b=Pjak+2zeyYLE5wRqFwHbDJ8vf5g8X+e3Ve3cqIl+ZOKueFXq8+1VlGji4WMSZNV1yz L2/TPDItWqYQ1X8aDdnpuj0+qoOr9ERfm5IWnIiFkLdS5ZGFTo5NWfAI012/+DRCJhrT eJ2xo7S3Sg5e4uXBSdBBP2gDTpgYGWKjfpodAvQ24IFsp59BZhg+gSoRFg/c+4d3Oa+k 3jluKAZKqYBZyHziwV/rcJPJCqyZbz4TTHxgtVRGDigNblbXvKiM/UjcBIzDAb69cs7t 7UccENbC3/TwXgmvL4OTQDegep+rcqsz53rGXWU61wa1H1W6Fw58HbXg4Q755kKcaCWc dbhQ== X-Gm-Message-State: AO0yUKWvOJ0KHcotGal91YPbxWC0jobuQVU1QFd9lwxj10ob/WDcCzme ISR6MaiyWUh5ifzuLJmgWQs= X-Received: by 2002:a05:6870:c111:b0:16a:7b27:71d3 with SMTP id f17-20020a056870c11100b0016a7b2771d3mr6234295oad.7.1676792012296; Sat, 18 Feb 2023 23:33:32 -0800 (PST) Received: from localhost (227.sub-174-197-65.myvzw.com. [174.197.65.227]) by smtp.gmail.com with ESMTPSA id u6-20020a056870440600b0016e49af5815sm3425295oah.51.2023.02.18.23.33.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Feb 2023 23:33:32 -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, arnd@arndb.de, linux-api@vger.kernel.org, kernel-team@meta.com Subject: [PATCH v10 1/3] workingset: refactor LRU refault to expose refault recency check Date: Sat, 18 Feb 2023 23:33:16 -0800 Message-Id: <20230219073318.366189-2-nphamcs@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230219073318.366189-1-nphamcs@gmail.com> References: <20230219073318.366189-1-nphamcs@gmail.com> MIME-Version: 1.0 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1758248029818236005?= X-GMAIL-MSGID: =?utf-8?q?1758248029818236005?= 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 | 142 +++++++++++++++++++++++++++++-------------- 2 files changed, 97 insertions(+), 46 deletions(-) -- 2.39.1 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..5f1d20a0a2fd 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,22 @@ 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(); + if (!lru_gen_test_recent(shadow, type, &memcg_id, &pgdat, &token, + &workingset)) + goto unlock; + memcg = folio_memcg_rcu(folio); if (memcg_id != mem_cgroup_id(memcg)) goto unlock; + if (pgdat != folio_pgdat(folio)) + return; + 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 +329,12 @@ static void *lru_gen_eviction(struct folio *folio) return NULL; } +static bool lru_gen_test_recent(void *shadow, bool file, int *memcgid, + struct pglist_data **pgdat, unsigned long *token, bool *workingset) +{ + return false; +} + static void lru_gen_refault(struct folio *folio, void *shadow) { } @@ -373,40 +402,31 @@ 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 +445,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 +468,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,7 +489,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(); + + /* + * 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); + + if (!workingset_test_recent(shadow, file, &workingset)) goto out; folio_set_active(folio);