From patchwork Tue Jan 24 02:11: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: 13113305 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 67F0EC54EB4 for ; Tue, 24 Jan 2023 02:11:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F3AD06B0074; Mon, 23 Jan 2023 21:11:23 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EC2696B0075; Mon, 23 Jan 2023 21:11:23 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CC7B46B0078; Mon, 23 Jan 2023 21:11:23 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id A74C36B0074 for ; Mon, 23 Jan 2023 21:11:23 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 755F91A03D3 for ; Tue, 24 Jan 2023 02:11:23 +0000 (UTC) X-FDA: 80388065646.25.40F2276 Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) by imf13.hostedemail.com (Postfix) with ESMTP id A46392000D for ; Tue, 24 Jan 2023 02:11:21 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=I2ybDYgQ; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf13.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.214.169 as permitted sender) smtp.mailfrom=nphamcs@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674526281; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=cRcWpKR8j+Ur7LPi1tUnqjlAf0HkEcdLuumL6I9JC7o=; b=ehCjGtGG0VjljM86gLHJ9hFubtX2jqYOcl0JKCbNZZAFSAs/KFVczhOrUAEb5Edvj2Chow sB7eouyLHQwAUndeQJT2Tvhchh3u+A18puVSQoeVrQElI1NldZrFOuy6cqVbwvbFQGf1pm 5O7OP6kEREFbkDLPjTWJL+sIYQ7elfQ= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=I2ybDYgQ; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf13.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.214.169 as permitted sender) smtp.mailfrom=nphamcs@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1674526281; a=rsa-sha256; cv=none; b=Ar8fANg/LuFWfk/Cr6W8tdY96k0/1FGl4UAXHH6tMu03/pHib1vbNWMrzcxQbMHNEKmfqx IMX1TRGDFU5pdJxmIEyeIK+fp62c9smyO4gcCCukIgD95fW1yOktrVN1HCZNzsPymfDUa/ CbHfjLwA5UxJ1AeLkWzEkXsA0WiO/7c= Received: by mail-pl1-f169.google.com with SMTP id jl3so13339938plb.8 for ; Mon, 23 Jan 2023 18:11:21 -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=cRcWpKR8j+Ur7LPi1tUnqjlAf0HkEcdLuumL6I9JC7o=; b=I2ybDYgQDja4idScpPWc9//gSPFmJsAY2maNHzanJ6eznD3iqfVg1VF06dyNDCorqW rGSYaEdeCCxMn+KvWOTWHVqKSdpyhDlM+i19X77qZMx6oo9YEk+jlQeapJ2YOmYgV3NS nUnHF/bvPHK4C4BCeQM5EINi3NBER8BP6ifA7NpvCWAknTPr9fXR2UAGbIjyUTZdCdTs pPIrUtiFutaLCBj/isOcA5BUwrbGNzVv2yKsKHMRdajVN4i4sLNhHgfzv4LgvJXLzw/q re+dbBTwLzNe8RnItPN3HYax1sp2EvKk8UgR6w1snBjXFuhC35jtXsPkc4ejwxL//SFQ MZwg== 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=cRcWpKR8j+Ur7LPi1tUnqjlAf0HkEcdLuumL6I9JC7o=; b=29IyHQOOxKkW+Cv5sRWn4WXjkphGCopekxDVKpJ7DT3cKB12ZmlPkCYrlsf/xIjqVj LqR/XvsfWwJoa8vREgBf6ssdAvVHbcGU+5VV7Ga7NXSJVp+cFc2Cm+9ypMZqWJ2/4Wxg HzvDFx4fjmWjq7wHLCQxjDZDTLiwohgk0WoDq5rfLqWsJNtiaNEidRfoRMJ47hDK6E39 nV45mwuBy7GJ8PrGaFa3hP3DrKDdK/UlcyN2bxnzgI1dhoH3pf1rjpeCTvV0+KzPOBh/ t940JsAh8nnUyqvqc1l7MUC0S+GUQOSj0naJ9loL4CKDxWLIGMRPh529GoPR6J6kQCfH LjIg== X-Gm-Message-State: AFqh2kqaNRwGn1UTfqyvZurSJLpGZkL6Bfic3vfXrwOAPZrmE7DAjjJ/ O85wtue89F3lY+wRB1fFFkE= X-Google-Smtp-Source: AMrXdXt/aCCUSGn4NbrAdFByJdNJtJPiPYrQi1+7PjBZ758At2+9kLZpMtfDAUaLIh+9snd1qzJ/Hw== X-Received: by 2002:a17:902:cf4b:b0:189:76ef:e112 with SMTP id e11-20020a170902cf4b00b0018976efe112mr30322497plg.41.1674526280522; Mon, 23 Jan 2023 18:11:20 -0800 (PST) Received: from localhost (fwdproxy-prn-003.fbsv.net. [2a03:2880:ff:3::face:b00c]) by smtp.gmail.com with ESMTPSA id b6-20020a6567c6000000b0047899d0d62csm218144pgs.52.2023.01.23.18.11.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Jan 2023 18:11:20 -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, linux-api@vger.kernel.org, kernel-team@meta.com Subject: [PATCH v7 1/3] workingset: refactor LRU refault to expose refault recency check Date: Mon, 23 Jan 2023 18:11:16 -0800 Message-Id: <20230124021118.154078-2-nphamcs@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230124021118.154078-1-nphamcs@gmail.com> References: <20230124021118.154078-1-nphamcs@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: A46392000D X-Stat-Signature: urqgy5ubgqh4hxmaxtdk7cq4i5a3jgen X-HE-Tag: 1674526281-532027 X-HE-Meta: U2FsdGVkX19Y6K/AH3GKs1Vt1wlKzGoM8ZdNMjQTl96DG0La6Vk8avAHB85fH/2YTWkXWFE5YPAnplwC9vTt+ytCEqe8CUtcOVHA7SqgZpqhd1fdpGWj8aBqYDteermjZk7F7ByG8ST6k/Mlvsmta89I9YJniOOpH6SQnsJUoM6D1noVdLqSC6+j416WnBnJmndo7tlsvC1mCN8g0mGzmdzPDLSIlfN6FjmBO4caJsPAq6c/SJFHsp/JA7obu2FqUfJCeQRp7VuDLjV4qMzMkYRLKsumkNtTbCMlKkTXmvhAiKklgFA4OP8fPBf0qu7YXpsWz5ultJngfMUFi9+++8WB2ZOa9gc8CkjNCFrnF37g7FnH0y+WVz1WZzU4/QY/C6PuvUMXuaAufsD5UbCs6ssL9owh31qtrTFG0tt6ovmNzJp/7dW2UQvftiExjmr04mF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-majordomo@kvack.org List-ID: 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(-) 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..2f2d94867366 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()) + 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);