From patchwork Fri Feb 3 19:04:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nhat Pham X-Patchwork-Id: 13128274 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 211D2C61DA4 for ; Fri, 3 Feb 2023 19:04:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6D8A96B0071; Fri, 3 Feb 2023 14:04:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 689436B0072; Fri, 3 Feb 2023 14:04:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5508C6B0074; Fri, 3 Feb 2023 14:04:18 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 42E7D6B0071 for ; Fri, 3 Feb 2023 14:04:18 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 01B7C1C6BB3 for ; Fri, 3 Feb 2023 19:04:17 +0000 (UTC) X-FDA: 80426906196.23.9B65338 Received: from mail-pj1-f53.google.com (mail-pj1-f53.google.com [209.85.216.53]) by imf19.hostedemail.com (Postfix) with ESMTP id 018D01A0029 for ; Fri, 3 Feb 2023 19:04:15 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=HwsMYazS; spf=pass (imf19.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.216.53 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=1675451056; 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=l2Qla7I7Duq+UJ1Mp3OlMfujz6rxLRhpzw4lXUxns0Q=; b=XJbouTpPszFa+F0uzVkrxs7cqUYMKQ0UINWChl/PQIkuQXqgb+h/yYzvIILgjNoI33wV4R MQo0ide45VDrPDZmaVAr1583FGZfV0BFo2LlBiCk766vCefN7vUPV70iPjqL3+m3IVFHLG FPrQts9sPERawy26IDFbC6SHBXqABDk= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=HwsMYazS; spf=pass (imf19.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.216.53 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=1675451056; a=rsa-sha256; cv=none; b=XFw80Z5h9/mk2t9CUyNgoxGf16Dxr0GYXOWA0f1Yffd1ndP49MeQZQYu2kusBjqUcc90Wa KY2R9l4vWP/Ib3+4lF+rURzQa/4PRThFdnT34KtOt9vgve7NQO+nxjzMJUqNqzc2AA21R9 7hc32SJdjpHWj5kM5NNELmQDBc3+AV4= Received: by mail-pj1-f53.google.com with SMTP id rm7-20020a17090b3ec700b0022c05558d22so5791594pjb.5 for ; Fri, 03 Feb 2023 11:04:15 -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=l2Qla7I7Duq+UJ1Mp3OlMfujz6rxLRhpzw4lXUxns0Q=; b=HwsMYazSFWj5Ss4irgfWFeLiGYVSYDAETzx/xwd6gADPebLaYY2Lu6GkERV3fZUbxU HqotSCvhEIYQdCj0p9v0SYA5nTyFEBkkIEOYVA8fz/BlhkJM3s7q7vWBH6oT5ksFlDD9 9+K5YxKa9EueDhfoYdvwBwxsl62mD9JTdh3leYTWL8n88nJ2HZSAqloeombAXcOGSnZ5 qPZ5iq2OsLENvF/cx8VZLPuxkrdKTJlWpA+zUVFLWAmQ7SPf5LEhmhFta2MyfetTxr/A G1mtn+SRa6/yxg+50EewCcZ5ufyIme+TfjUzj0eOs8qPto6sdoCiYrDz5PwZ9KCacoQP gbdw== 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=l2Qla7I7Duq+UJ1Mp3OlMfujz6rxLRhpzw4lXUxns0Q=; b=8OCWB6p51ZLiNUTkQOxmlLcKNqvmNhr0pbS+vdcVmbiLsocGlLm8aYp10w6pvS8K9N rYofseL2gDAZ+L3Vqm1+vbfl/nNYVQgDyJPf6QU9uIeJQVA/GRzbsTBXZihhiX7G86M+ UVnYQ7b6QlNxSqzsIIJKEixMCwoy3GWURYtJIStIEoJNNdgZETEU06WFA1s9cLBeUjrc fZ9SQMXUpXWMG+zUiv67TYend7FXpExohusziqv/rH8eJ6cnB2ALm7AElvfQqMUmpRL6 XFWQ3meSr6GOX0zukDpVqUJndDf8bCWqE3sf+O7jY6PhKrEfm2d91bxrRK1GCSaMusMv edEA== X-Gm-Message-State: AO0yUKV0J493uAHlvZhFXwzJQx9NL4NyoG7DKGggtsfd1uL5A9HpNlh1 9iGvWrdXZzZvBYeMj3nWX2M= X-Google-Smtp-Source: AK7set8fmFZVJshKZthm5lkec3wEMqp3U6Vckuu9lmM9sq8EAYpsCygfsgHDWDY69J7iHIQ4LAB9Rw== X-Received: by 2002:a17:90b:4d10:b0:229:ee75:5d09 with SMTP id mw16-20020a17090b4d1000b00229ee755d09mr11792635pjb.40.1675451054769; Fri, 03 Feb 2023 11:04:14 -0800 (PST) Received: from localhost (fwdproxy-prn-003.fbsv.net. [2a03:2880:ff:3::face:b00c]) by smtp.gmail.com with ESMTPSA id l11-20020a633e0b000000b0045ff216a0casm1852791pga.3.2023.02.03.11.04.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Feb 2023 11:04:14 -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 v9 1/3] workingset: refactor LRU refault to expose refault recency check Date: Fri, 3 Feb 2023 11:04:11 -0800 Message-Id: <20230203190413.2559707-2-nphamcs@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230203190413.2559707-1-nphamcs@gmail.com> References: <20230203190413.2559707-1-nphamcs@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 018D01A0029 X-Stat-Signature: j3xu9nei8j5sfhg9f173wjpdkj75wtdo X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1675451055-865969 X-HE-Meta: U2FsdGVkX1+K/0LW6w32CvYdB+MgL2MGhCuq7FSdJ+Lg8esuKOFiq35z5djt30aGMRhHleCGzvvpBPJSEcMLiJtxhaVLwMrmFv+M5phqGR5jxFz24zEl0zdy9yKROJ5yNw9NVHRKGnZbcY9HG4lbQ/BoAh4xd1pwwbJcKSLik4vWCq3gKdwdqTSHhexeHL0ph3IvRoCdqC21WE7GWzC9+n9qVmP4VgxBQg3neG0D7u4ZQr7aZl+B8pAXRsQPF5tl4JYeRLwNEXsfMW80U/UhkaJKp49zCHNCQ/zptzpYto3edGbbAO+wvnE8dn/cefoJgBuZi9fQLBS+VTC8Trz2FB/c+dwiZeaFFu1CePHOz9Al+TyEJ1NuNWsE9az30ljm9YuBVEjR9PjPFPmpEyGMW2/L6kqqxR0c4PKiVk9B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m@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 | 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..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);