From patchwork Wed Jan 4 23:11:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nhat Pham X-Patchwork-Id: 13089186 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 277F7C54E76 for ; Wed, 4 Jan 2023 23:11:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C24D7900006; Wed, 4 Jan 2023 18:11:32 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BAE36900002; Wed, 4 Jan 2023 18:11:32 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9B2DF900006; Wed, 4 Jan 2023 18:11:32 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 88D52900002 for ; Wed, 4 Jan 2023 18:11:32 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 6A1821C5BDA for ; Wed, 4 Jan 2023 23:11:32 +0000 (UTC) X-FDA: 80318665224.24.2779498 Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) by imf12.hostedemail.com (Postfix) with ESMTP id B628640008 for ; Wed, 4 Jan 2023 23:11:30 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=jyGISYJ2; spf=pass (imf12.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.214.176 as permitted sender) smtp.mailfrom=nphamcs@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1672873890; 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=YRguhEw3CY77NjO4ltDj62CfZtUxun2rreMj0XQVN6o=; b=aNjYqVa5BwNC0fRsZ2jwJFpzdZNdoIjsWbEgDhI6XEkoiISzR2kEuwjj3uMRf4TWeZn6h1 d7y0Ly4Uaw+r2rFD+5+YwVKzS8CKd18Ly1td/t9B9V0UICWN9OJVuSKE4Az/zKdjcSUbDA c6HIGockaZbb+hBbWXGBZeZ+zxJnuJE= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=jyGISYJ2; spf=pass (imf12.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.214.176 as permitted sender) smtp.mailfrom=nphamcs@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1672873890; a=rsa-sha256; cv=none; b=dvehl2+UEfluFqOArwcbWX/qkq9tW1ctR8Exk6j/EYUDajrqEg+4KNajlTEneAK/oeMCx8 m5nXqOs5TghE2EIBOQFKNsw1wwut6EUC9o7XVXOBh8KbJ7/BuesmDdH7N7MemspylbirNV vRfPuipzIuLggw+GngO5RqkGVxduOKE= Received: by mail-pl1-f176.google.com with SMTP id c6so4343928pls.4 for ; Wed, 04 Jan 2023 15:11:30 -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=YRguhEw3CY77NjO4ltDj62CfZtUxun2rreMj0XQVN6o=; b=jyGISYJ2bXyxzPDjNt7gxgYkA3DkW9H2LWEHrzF7u7HY9OO3ykASRZthbR4Z7gtykp PbETPH6V93tcnDRDmgJlodPL0AMKLQCSVa/+VrePyM8ivqHevPbrC9f8lVNIUrKPxZfu KfV+J3/xZfustyVtc9UnMMKKNeruDKDW9b9hEqsFdXNFtQKg1gvvfwcgbiHy157fWEpv yqhiNvouTjFyhExBFgrGLGLEVsSTgFzXIB+Wqr8/pNJ+lg4WrO5tMPbaC25wyuDU02QY eC9CVsxpRUej958Nl5rCGDigD6Kx/L3Ls7iPJSgUdPcSS89Yk1sHnhhcJvZQ071QyfDf LpVw== 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=YRguhEw3CY77NjO4ltDj62CfZtUxun2rreMj0XQVN6o=; b=51spH/eFivYFpTkAlHn7x6gjB7/8l7ICJRYDpVgpAMt9yQXjhozExMXM360CkYUuxQ Ozygz6aLD5fRgpYQ6aCu4SpyQg1JGBd3vJ+xbD0MedQqYfnPNJoMZjSCChcz80lFactI oQhUGEduvL5v98myXwrR9t80BODSZi/yBOC1znQIJ828CA+G89FoKQ8jsJuOZ4ROKIVW PQBfXOJy6jAtn+mnmSTHqn3k2J11TqJoL8i3lvXNKUFH84DgIRJjeDEFplSUqkw1E6S6 RylYKar84DY7xc5ZZnv+x26RNqio5T9ma/EXL2nyyqEB964RmjG+QU0Z995jEzKQ68uV rO9Q== X-Gm-Message-State: AFqh2kog9SZJF41b513BNQISD+FyttHPOWlbPOMn+Yytoa3SdgRy/NcJ 2JOmlpRNmjKloMieaD7R0MY= X-Google-Smtp-Source: AMrXdXtDN70ygF0JtnOTJ8hcXpGZ6iD+t51dv+CIaS3os7HPwEJiVoNugbtnb/ttfyqE+Xsf7jXGkQ== X-Received: by 2002:a05:6a20:7d95:b0:b0:1051:2a96 with SMTP id v21-20020a056a207d9500b000b010512a96mr76299860pzj.57.1672873889625; Wed, 04 Jan 2023 15:11:29 -0800 (PST) Received: from localhost (fwdproxy-prn-002.fbsv.net. [2a03:2880:ff:2::face:b00c]) by smtp.gmail.com with ESMTPSA id z28-20020aa79e5c000000b0058215708d57sm9993540pfq.141.2023.01.04.15.11.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Jan 2023 15:11:29 -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 v5 1/3] workingset: refactor LRU refault to expose refault recency check Date: Wed, 4 Jan 2023 15:11:25 -0800 Message-Id: <20230104231127.2634648-2-nphamcs@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230104231127.2634648-1-nphamcs@gmail.com> References: <20230104231127.2634648-1-nphamcs@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: B628640008 X-Rspam-User: X-Stat-Signature: zctx9npsixqc877sgg73yqtinig7iccu X-HE-Tag: 1672873890-696192 X-HE-Meta: U2FsdGVkX18yL2rSmwUXdv7iGqYK4uABi2L4dY2p9ZndQL0oX6ab5tgFwpSxJihESb6xJIz9De7ik3Itm89nJ2MhZgGpl+vODBCLjY7UOD2DZ8kic+LVkk4c3BS5AmlFmtn7a5Q1DrTASYhtRfnmUyQbuIOzQE7KwegwCguCKvoCBq/UopdHCusZ8s5JmMQlDQEtnChYgW3eMtJK+Gv/GkFPc6gOfN22UBzkgx4MP4HgIqHxJ6/eU0uXq/FbIbf/EJhgqXyA7DN3iGr/GdWaKNoGjsuFxWx3KioKz7F5q/sRd9FXeL2UDO1cAAp86zV7WAChxlB+XeFzYo7VF/todLPcfxOb+5OrJqg83P73CToEiidlkj7Q8L9g8HfHxw4y19GYPwcD4V7pk9g80ccp4MwSWu6AkZdJp2Kd04avRauItYdZkh9wQJM2SDiQbmD6ZA9dEp/UAC9aolPTvQXxkZoAU603zROTp3DzISObhy9Bhzj6BNU5C4GpbWpGXKGiwAFHWgosK+RsXapJrCma82mkHptJK2uk2rb9fqZBuhGAYKwhJ7E34rOV6F+OzTKaYYIOkcshd9FE3XmJUOgOD7uuWpKdUPozF4aBd5Aul5nhZX1visMHZKLa9Ay0MmloGKdWeH+dVkUNdpdZmOFGl9l/YbmGsojMfA9vdMswhxAVUmLEfoUnbRR2Ix7vP1yCHlb4wrv799gYjWK5M8lqULgCkWcw6aOaN1rafkgCXw3ukTXRi3DnN5yH4z2oRN3LMmWxUER7orKut+Y45EfBNw9w+f1CdYY46WL8HUcTbSPZS0IbeOFOBMAxjwt7UFgw2jSoFJBPPO9LUV4BWm+sQVpTF6rjOEytX+541HYsoUG9NgKp3WKFW09DW4GCtIXmo4IDihAQNdbfHv+OEazQ42Z7uJzqy7gPZKDZg/9exCoy8OjgqsZwArtV4/tX+Cb5CJ4wePC4Zqf6uLuBBRc 2x1br3Ph 9WJqSuMsF5mQ9BxDDhsIAnLWX3/FTjM36EKbnIcv9a7LJlO3ty0bSvkN6jX24dSFHi6SNd6crlcOrDG17u05k9Ld1bUKy+g7ZjJUtv7YGxNLABkqhk7ZXcFBUx4Kj2c3M5S9R X-Bogosity: Ham, tests=bogofilter, spamicity=0.000055, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-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 | 129 ++++++++++++++++++++++++++++++------------- 2 files changed, 92 insertions(+), 38 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..006482c4e0bd 100644 --- a/mm/workingset.c +++ b/mm/workingset.c @@ -244,6 +244,33 @@ 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, bool *workingset) +{ + struct mem_cgroup *eviction_memcg; + struct lruvec *lruvec; + struct lru_gen_struct *lrugen; + unsigned long min_seq; + + int memcgid; + struct pglist_data *pgdat; + unsigned long token; + + 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; @@ -306,6 +333,11 @@ static void *lru_gen_eviction(struct folio *folio) return NULL; } +static bool lru_gen_test_recent(void *shadow, bool file, bool *workingset) +{ + return true; +} + static void lru_gen_refault(struct folio *folio, void *shadow) { } @@ -373,40 +405,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, 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(); }