From patchwork Thu Jan 26 17:53:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nhat Pham X-Patchwork-Id: 13117562 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 18E8BC61DA3 for ; Thu, 26 Jan 2023 17:54:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A73116B0075; Thu, 26 Jan 2023 12:54:02 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 934466B0078; Thu, 26 Jan 2023 12:54:02 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 739958E0001; Thu, 26 Jan 2023 12:54:02 -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 5E8916B0075 for ; Thu, 26 Jan 2023 12:54:02 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 27F2F160F19 for ; Thu, 26 Jan 2023 17:54:02 +0000 (UTC) X-FDA: 80397698724.18.2005AB3 Received: from mail-pj1-f46.google.com (mail-pj1-f46.google.com [209.85.216.46]) by imf27.hostedemail.com (Postfix) with ESMTP id 1DFA040014 for ; Thu, 26 Jan 2023 17:53:59 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=PU0cFraL; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf27.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.216.46 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=1674755640; 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=IpEt0pI4Bn9fGgg3CDzHcKBQle1j5LKxQUzHlnQVQKy0KiUp2xrLc7kUii1jfBpIWk6h5P qhHcWpYHCgTJiv+9gKH98OWTvZ6VeML5SbGnufvPo58IVmryUI8FenqqzlsnCyAE+v1svI 1QN4quLlSgAQ1GTaakEsBuy1hv87Q4w= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=PU0cFraL; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf27.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.216.46 as permitted sender) smtp.mailfrom=nphamcs@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1674755640; a=rsa-sha256; cv=none; b=aKSWnfQh3HwARSMMrDAJMrU+9WHcGm+0sbHahbPaPTYE6kFS8n6Ip9qzD686KhFO8FWD/7 rGmpTkf2tP+/9LTLerYQUSI3l1DtxuNKatCpLrpaT5Wck3FFl+2Cyu8Gb8/WE7FJXLzTuS Yx+bapXgl80/aaXdq6mrdfXHjUCdNCk= Received: by mail-pj1-f46.google.com with SMTP id z1-20020a17090a66c100b00226f05b9595so2524495pjl.0 for ; Thu, 26 Jan 2023 09:53:59 -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=PU0cFraLkBQHmG6k8BceQUnlKXiB0ESp0AO4vNxqRRemq54IBBuKglvIcBXp+OM2io hodn/SjfhmHSvubWptgZvJNSmefHlYfvT805uKwrxs69TFDYMZAsXUPEwRqLQYChVIsU x5vlIUgwI9392dDY8cirPQg5Ie+3w3+g4H/F2DKV08cmyA9fbjxZPedgBpjN1e22lkI8 GTqpR9DMbdviXsQSsm00zCf0qdez0uqG9tgRZ2yXgLo3UH0fXqfhVLNNIp3HNNBq358W Qev3XTLLGkqSzOYgoeZV3TXrOcBT3w7DOaDrT1N9/tc734akha14UGNYjMpafPJEwb+z 59Rw== 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=5mfJ4kWjC6VHt10DwlNfIAJ3IgxJ/6lRKIda1e6/gxaALdfl8Oe89Pmad9sco2w10T 8hJ3b9yvzs7lzP8YxbKl13P7PaIm/39Si7CI//8RZlRgcwlWGrifjD6QtkRjPZlDID6a qntLhY37LBLN33rEuTgOhaBvWVMBNddRMKv/ICgixlqSTLXrf/pAimp6k3N2nKmu1xmC Bcb3XrHwrS83CVw3MVmSsSOMZq7Ue34aBsKkuWCspNvEQo3naDMIJU4PQgO1rZ69q5h5 VLF2TcTDw58SUaMYnhk3jXa1PrSwdE59bxVGbEDylx5/OLgc9ZzWiMdLuuT1Fhdlt18p CD9A== X-Gm-Message-State: AFqh2kp86iyvE+S8kV+dUbIjE6FwBvNfvAtR79tvS+xUfWp05y7KAIRy soyZUESu7HvwxhggnVb7qwc= X-Google-Smtp-Source: AMrXdXtT2JnYGzbKA+WV0wM+FaZVtJPk1sjLRg//hOlVKp8fVWtxFE7Wu6V5yxBVpJInPJNJNzP+Hg== X-Received: by 2002:a05:6a20:4ca1:b0:b5:f6de:e84c with SMTP id fq33-20020a056a204ca100b000b5f6dee84cmr34575199pzb.42.1674755638880; Thu, 26 Jan 2023 09:53:58 -0800 (PST) Received: from localhost (fwdproxy-prn-008.fbsv.net. [2a03:2880:ff:8::face:b00c]) by smtp.gmail.com with ESMTPSA id b24-20020a656698000000b0047915d582ccsm1016661pgw.20.2023.01.26.09.53.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Jan 2023 09:53:58 -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 v8 1/3] workingset: refactor LRU refault to expose refault recency check Date: Thu, 26 Jan 2023 09:53:54 -0800 Message-Id: <20230126175356.1582123-2-nphamcs@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230126175356.1582123-1-nphamcs@gmail.com> References: <20230126175356.1582123-1-nphamcs@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 1DFA040014 X-Stat-Signature: 9d17z55f4qgb34grrcyzu3f8wchtzzr7 X-HE-Tag: 1674755639-593352 X-HE-Meta: U2FsdGVkX1+KNdXP0kf99rgLURn9jVAPHvIun797/AE/HZ4Ul/rg9sCDe3i3YM5fWGFBIFQGFEMejp3p0war5WkC8YUtUil+h2EHm2UW9C6ir2DTZMbQUYzgh1t71fIBrFsNIx0TBUGwkRjIo2LDQzwh/35ngOhIoiSsSodi/+jirYYkQuEe6td9ZxPlUwLtqGdc5SqdMvgLIt71MlkcbnNG03VXpCOXVp12y5HREt6Hkoj5fLYHasKIymTP10+BJG2O9zQVo+DHs0s/9tSJ5KWB90nGOT88VQzb6ppbWYxYza6bg3uDcoKenXezjrBvfXpO+4Ae5Q7Gn6REBizcKAkgt9hNGhlCv6CSCvyZTJESzAXjLbX6r4Jl9sYzKC5Ytl0U47MpcnlOtqCgN7wnfhLslZCSvQHuY84L8YJdoJ9p1frO889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: 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);