From patchwork Tue Jan 17 19:59:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nhat Pham X-Patchwork-Id: 13105017 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 EFCB6C3DA78 for ; Tue, 17 Jan 2023 20:00:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 85BE06B0073; Tue, 17 Jan 2023 15:00:15 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 80B2F6B0074; Tue, 17 Jan 2023 15:00:15 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6AD056B0075; Tue, 17 Jan 2023 15:00:15 -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 59D566B0073 for ; Tue, 17 Jan 2023 15:00:15 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 2A7F014023C for ; Tue, 17 Jan 2023 20:00:15 +0000 (UTC) X-FDA: 80365357590.25.0CFAB90 Received: from mail-pj1-f44.google.com (mail-pj1-f44.google.com [209.85.216.44]) by imf16.hostedemail.com (Postfix) with ESMTP id 55200180010 for ; Tue, 17 Jan 2023 20:00:13 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=KzYiQSMT; spf=pass (imf16.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.216.44 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=1673985613; 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=1rtklvkPrCcJMB4HbTLH+CTMxTM9mavnALzKKZKS5/pu8GBM9dg0CxQtppgbKjGwdjg8Dx WIOy101lh2QxDlni/ubXWRmMIl17/gtBQH0NmsHFLtzhnDRd66qE3mUJkoGZ/XtMy9Q6Bc O9wSIwIcDiOaH+IbkoONMrLPXeUp3JY= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=KzYiQSMT; spf=pass (imf16.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.216.44 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=1673985613; a=rsa-sha256; cv=none; b=18q+BfPMoDsnObLCfnxzJ324b09WNit9bG5QSl8CmYkNagxA9yR2sBz1rDBaGDd2Jl3jk/ tdVsXDYdCkz+zTMstfx4WsVNaBQCl3U8mfqmOurz/VmnpuYny9rNrRpq0z/iRKqG/ABUvF /dfiThRGg4zahW/M4MKe3V2Gt2WTisg= Received: by mail-pj1-f44.google.com with SMTP id q64so33516538pjq.4 for ; Tue, 17 Jan 2023 12:00:12 -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=KzYiQSMTAKyHXNw/WCFwcPITqMFvnZG/cOSbCHgWdZkCPLFpZu+3lLrBOny0CUX9YX 6E4VMIUPg5bJQCZy6jid8Njwr3mMUw7zJOPH8NHbf15Pc9hKSgVsY+rh2tIMLBCQjA4p RCfOUA54sAjtFOTk0auPkj7vERAY4lSxn8qfDVvK/NVytSXYa4dq8ph4HT0eIB61O/Ho PglANgzCJKu985Me1Vf9J7pToUtQ/wt6HyosQQudO0zlAY4gCNJChzrrjTp3K1rbPZ3g IUZsq2gBHWG1NDTnDev4zunqNcU0zoL0weMq+cY2AxHyrWGYy5tsijqAWcpQzwjPPCjT z6Jw== 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=PuMhsjsAb+VAEl66N8w0nO7eP5rkjKold57gmNmfi2B/ywbbzJ7gMZDspDzwY9SgwM lR7+OjyWCBwKWIQkASpRwIJ1/yanVL0c+DQ8/eqK4uT0Aw4FY96dYDo91Rgq16xTJoC1 We/XMp4JbDuJ9fRVJ0MFuRkRyKo9AiR4mSElzyNz9XAn5JFXhZnuEZdXMaj0CfgZUXVH Ufc1/5pNK5K+O6F064BlZf0lxpUjRttBjiuboUXq33l1lxO4PrWCqSCR1KmQGB0TvY/z ldKUx7OlN2wRKSS+st6Fxb2tnCDrXWqyqVyAqYHrU1RPUVBHp1KjshWrZ06eFJlczjg2 OfTw== X-Gm-Message-State: AFqh2kq2zNPlUOr6Uc4wRx8cpDYBOk/HZRup8gMxbsGChgsV/zZdIiwg rvQzRbMiuw8gvkCdp8chX10= X-Google-Smtp-Source: AMrXdXtIt9YB1XnCzS+dTfzT9gh3KjT7wZXOO8uJT5f8nQp8Rict742HP3JpsDNbdyPRX0PaTdQ5bw== X-Received: by 2002:a17:903:2154:b0:194:a371:716a with SMTP id s20-20020a170903215400b00194a371716amr4184867ple.60.1673985611955; Tue, 17 Jan 2023 12:00:11 -0800 (PST) Received: from localhost ([2a03:2880:ff:1::face:b00c]) by smtp.gmail.com with ESMTPSA id y21-20020a170902e19500b00176dc67df44sm43037pla.132.2023.01.17.12.00.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Jan 2023 12:00:11 -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 v6 1/3] workingset: refactor LRU refault to expose refault recency check Date: Tue, 17 Jan 2023 11:59:57 -0800 Message-Id: <20230117195959.29768-2-nphamcs@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230117195959.29768-1-nphamcs@gmail.com> References: <20230117195959.29768-1-nphamcs@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 55200180010 X-Stat-Signature: i3x7k54511cm1sstg148jhzs347645qy X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1673985613-648602 X-HE-Meta: U2FsdGVkX1/glWe4Pgavw4PA1qNKu/Sp0zY2hkU18qo19bSvulw2TDyeY782wcrEVoDXAnbuhHU8o4yrc7G+ikj01rES8EqElNgXnmfop+gkRXedwF8VFYjdMkX7OK2CxMgrYtZZF6w7eadG0qiq1wKX3TpGUXr7b3wbDkaar2zwNrRs4O0HvMsBcjjOrCknpV5ByCWEOkO1g9PUP/Mx1pjpjxmpe/uKJgVMyFNN4dvr6F5M1nZHdxeiNk/UMXG8iEemWUEXdMNsbnbBm5WnE+63GonQxxGLept/VLsfrSv4YP5w0Q7HL/No7TZFKPLVfwXcP7oP9e8DcPXbmQOH9wygVDkU7oYDpeWOiwSF/3U0atnsE/MqedRLlCdZ5ofguNuBRBXW7uQ2N83FQpQeahg7kW5wUev5sRnlMNwttPHMfmOK+pqf3OLvn2p1kQS0VpAH1MqGSI0A/2IJTUq3iVUyqDVxHu/o5v/3DoPymA237xodepuJWB7seQWwL9UbgeJFSUKP1vZlSTtSNgyUzLsE021fqhOHxRevQgdztnNxRYKjUo9v/Qx/PVVMP4FuocrekQQvVZp+veOT5WIwjQpdQzcVqJ9CRdnmCBMb3h7uYECASdnk+4ZOvmO2fsH1yNxt+jWdMLtMP5gHfyUHVoPD//p9v1jNQWjhrGXhZX/h35hLhVsIjAglgOb3aLDpOUnYMuxeC7SiU2/ML8mnB2CXxD4UXLBg6fR31ItOTMqyNTUi7hhua59VkmD+iKotNBI3YgqGBS7XaTCn0BqxX1tCX3djG5fZXet7HV7qOxw4kiRh88cV2ywaeYFqkgLjpgyht37wBTqWvFkZfzPmR9AbKUMMnfPcxVjxwXnDiA8dBXxeQ+an9/3waigoLmgbKGZngDlQ24urQgzOOb9Zdxf8XZaEflcH62eLmKMzmOmvvA0+0WCEn5+Up3IZZGPLWEVN+mEIcBZ/cJmcPJf 6ru7xccx ikOMHNiKwaOQY6aB7lpUCOL7G7dV7WLp2iDb+DbI1X7NhKSQN5HCrR607zBF9LDFBGM4leeORI8Guh/9B0SCOxnlr5GmEk0SnuQj3T2Acn1tG2JgeNlXJZ+UnzNyPJtFfArOb X-Bogosity: Ham, tests=bogofilter, spamicity=0.000002, 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(); }