From nobody Thu Sep 18 14:09:52 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5DB20C4332F for ; Mon, 5 Dec 2022 17:52:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232441AbiLERwT (ORCPT ); Mon, 5 Dec 2022 12:52:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44244 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231783AbiLERvs (ORCPT ); Mon, 5 Dec 2022 12:51:48 -0500 Received: from mail-pj1-x102c.google.com (mail-pj1-x102c.google.com [IPv6:2607:f8b0:4864:20::102c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 445B71EC47 for ; Mon, 5 Dec 2022 09:51:45 -0800 (PST) Received: by mail-pj1-x102c.google.com with SMTP id fa4-20020a17090af0c400b002198d1328a0so6620880pjb.0 for ; Mon, 05 Dec 2022 09:51:45 -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=npFjL6Dds3DKnQo07iUTlbqXMaucW9/yvERNl8ZNQRo=; b=mLWxUj1chMRiHdvugVEpnaxQ41A/ablxwsfxwk8xacytULj+ChB/06IFBL6WwSIScd 4UnPBnLjtdJHcrXsK8FrHNSCKlQJosnldDtaMV7Y6yjl6vVGA8CnsUR4UNcov+bpxf7l 1e2gADjhBINCiJVwtfuwZmxFtzvnoNpDHz6DRdaKJi+DPqLQ8hCNpZBPeDqAKBn8P+67 AgBeur0PkHavY/K9nJOKtSb29BTgBnokiSezvSqrDesT1h0xSPvN4CjbmVS/EHS1/MJZ GilTo4H194oDwlt09T1aymIMfQ9aR7xVQAtH5wbx3oi6kb6dFhMa97eaikt7FES2sMrR DhQg== 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=npFjL6Dds3DKnQo07iUTlbqXMaucW9/yvERNl8ZNQRo=; b=ci3CrBJNXKRJICpECDdFlc8Q+DHLc7j0wumOdqGj5VNNd/8XuzU7qScBw0BvMU/7Su XXX3LnS5w7PinK/IB4Xq/EpIMbzPs9l2sfrdrpoRRz7QFZ5bL5v18rFZwhLmUs+tKhKk 8b8EB9XZ0rWeKNwNHRpC66KlYeLUo7hcaRbQE7eCPXysC9z3nmkA0JvPzGQQP0hRrs32 mnahJMU4S7wx/AAPxVjpDDvyVGyJcV5V6yTm4agI93uwRb4x8edxd1jp0odFgveGXxQI 5KqzlgjQQombQ9rI8BUoVwx8OZjAGpW/vFwRe4UcCHh172QaSCtYplqzCKUD5Pd5jshj +cdA== X-Gm-Message-State: ANoB5pmKqNDhadAlLi5yd/GGcst/pg1n3VY/cruRiMbX8e0hSVJMU8Bu u0ZSrNic2ihZmh1KelhBBuE= X-Google-Smtp-Source: AA0mqf7E5ztRL11sQZw9Ri0S+eLl0oSX/xxG2eP09s/4GVm7agnok9htqDogdvSs9ZdZLmqIvUrxvg== X-Received: by 2002:a17:90a:eac2:b0:219:484:e955 with SMTP id ev2-20020a17090aeac200b002190484e955mr53314897pjb.214.1670262704724; Mon, 05 Dec 2022 09:51:44 -0800 (PST) Received: from localhost (fwdproxy-prn-001.fbsv.net. [2a03:2880:ff:1::face:b00c]) by smtp.gmail.com with ESMTPSA id m1-20020a17090a5a4100b00219396d795esm9476586pji.15.2022.12.05.09.51.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Dec 2022 09:51:44 -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 v2 1/4] workingset: fix confusion around eviction vs refault container Date: Mon, 5 Dec 2022 09:51:37 -0800 Message-Id: <20221205175140.1543229-2-nphamcs@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20221205175140.1543229-1-nphamcs@gmail.com> References: <20221205175140.1543229-1-nphamcs@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Johannes Weiner Refault decisions are made based on the lruvec where the page was evicted, as that determined its LRU order while it was alive. Stats and workingset aging must then occur on the lruvec of the new page, as that's the node and cgroup that experience the refault and that's the lruvec whose nonresident info ages out by a new resident page. Those lruvecs could be different when a page is shared between cgroups, or the refaulting page is allocated on a different node. There are currently two mix-ups: 1. When swap is available, the resident anon set must be considered when comparing the refault distance. The comparison is made against the right anon set, but the check for swap is not. When pages get evicted from a cgroup with swap, and refault in one without, this can incorrectly consider a hot refault as cold - and vice versa. Fix that by using the eviction cgroup for the swap check. 2. The stats and workingset age are updated against the wrong lruvec altogether: the right cgroup but the wrong NUMA node. When a page refaults on a different NUMA node, this will have confusing stats and distort the workingset age on a different lruvec - again possibly resulting in hot/cold misclassifications down the line. Fix the swap check and the refault pgdat to address both concerns. This was found during code review. It hasn't caused notable issues in production, suggesting that those refault-migrations are relatively rare in practice. Signed-off-by: Johannes Weiner Co-developed-by: Nhat Pham Signed-off-by: Nhat Pham --- mm/workingset.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mm/workingset.c b/mm/workingset.c index ae7e984b23c6..79585d55c45d 100644 --- a/mm/workingset.c +++ b/mm/workingset.c @@ -457,6 +457,7 @@ void workingset_refault(struct folio *folio, void *shad= ow) */ nr =3D folio_nr_pages(folio); memcg =3D folio_memcg(folio); + pgdat =3D folio_pgdat(folio); lruvec =3D mem_cgroup_lruvec(memcg, pgdat); mod_lruvec_state(lruvec, WORKINGSET_REFAULT_BASE + file, nr); @@ -474,7 +475,7 @@ void workingset_refault(struct folio *folio, void *shad= ow) workingset_size +=3D lruvec_page_state(eviction_lruvec, NR_INACTIVE_FILE); } - if (mem_cgroup_get_nr_swap_pages(memcg) > 0) { + if (mem_cgroup_get_nr_swap_pages(eviction_memcg) > 0) { workingset_size +=3D lruvec_page_state(eviction_lruvec, NR_ACTIVE_ANON); if (file) { -- 2.30.2 From nobody Thu Sep 18 14:09:52 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A29B9C4321E for ; Mon, 5 Dec 2022 17:52:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231436AbiLERwm (ORCPT ); Mon, 5 Dec 2022 12:52:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44380 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232564AbiLERvs (ORCPT ); Mon, 5 Dec 2022 12:51:48 -0500 Received: from mail-pg1-x536.google.com (mail-pg1-x536.google.com [IPv6:2607:f8b0:4864:20::536]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 777DC1EC5A for ; Mon, 5 Dec 2022 09:51:46 -0800 (PST) Received: by mail-pg1-x536.google.com with SMTP id 136so11148843pga.1 for ; Mon, 05 Dec 2022 09:51:46 -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=DgolFyTl0+E6DqEY3qmBWlaeGZA7Win71lyPPhahR4w=; b=JLjQdVHYnbMLvg/MZf1ZTWToimKDYwAtFuiUWucB+fDvYlxKAHErVa5KYmlmM/EpS9 RT/5relmWtbdIgrAGRhfpKeiQUyaR7jXbSnX5+BRW0HmE2pU87PcGCHhAXOgHrWvYHdF GdfV2SAlbQU3XYAfWbYomKa66IMWw7xbSzCy43uGlFRUq4hOjkRDZrqf5HngpeEpY7Xz U2Pn9HA6G1iZNDSRUMyRC5bIAJKR1ocXNoR2Ipl9eRpK+V+2eBV+jvbBRYLZMe3IeqVs dD34GhmVEPv55SFLfTRLTcJ31Zpq65QWrBTB5OAT1z1/FHEbvfqgr48fVzmTWHwjph3g n4ew== 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=DgolFyTl0+E6DqEY3qmBWlaeGZA7Win71lyPPhahR4w=; b=DFNRsWBDIoQf+cuJSND36eqrECHXK25KdlGYgzRHxVoWgha9HGkF+zlT/fhU9dB9Rs BInGSCasHs62tRZhCJ8mC4tp0TqbCsovitFZtxbd23qNbXINMm+zToHOJ60qOxSzRoD4 Q80aCT5g3XoR/Visq1WaHvOBCOLj1E4BlrKJgqDqqZvqZxG7CqxaKb3PZlm3BAhyXW+B sKYJyjiavvw8g6Pnj55N+WKYaG+1ond07s7jA8HF5kR4ZrvE5DuFBW4OzMkH7gNR75Xd GiDUTZ6Q9ZkaSiC9RchxLCopoT4QNdHZ6X+vOYkctv/SVhKfPmsBXUHenWsdiHNdqg5J UKog== X-Gm-Message-State: ANoB5pmVgICGvXgk6hKXDMzfDuABbX5h1Ke4bCI2PhIHZTwKlWid7MLF zmSI8aMpcDJRxEsLwnoVgKs= X-Google-Smtp-Source: AA0mqf7oMYY14v8deqzRivQxSZrzLcOpdgKFoa97rre68AHddtQwSmZTp4ofxwVWVLR8XLEE6Z//Lg== X-Received: by 2002:a05:6a00:4307:b0:576:ba28:29a8 with SMTP id cb7-20020a056a00430700b00576ba2829a8mr9923939pfb.47.1670262705906; Mon, 05 Dec 2022 09:51:45 -0800 (PST) Received: from localhost (fwdproxy-prn-019.fbsv.net. [2a03:2880:ff:13::face:b00c]) by smtp.gmail.com with ESMTPSA id b7-20020a170902650700b00186b549cdc2sm10904824plk.157.2022.12.05.09.51.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Dec 2022 09:51:45 -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 v2 2/4] workingset: refactor LRU refault to expose refault recency check Date: Mon, 5 Dec 2022 09:51:38 -0800 Message-Id: <20221205175140.1543229-3-nphamcs@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20221205175140.1543229-1-nphamcs@gmail.com> References: <20221205175140.1543229-1-nphamcs@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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 | 143 +++++++++++++++++++++++++++++-------------- 2 files changed, 99 insertions(+), 45 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 *f= olio, 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_pa= ges); void *workingset_eviction(struct folio *folio, struct mem_cgroup *target_m= emcg); void workingset_refault(struct folio *folio, void *shadow); diff --git a/mm/workingset.c b/mm/workingset.c index 79585d55c45d..44b331ce3040 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 =3D mem_cgroup_from_id(*memcgid); + + lruvec =3D mem_cgroup_lruvec(eviction_memcg, *pgdat); + lrugen =3D &lruvec->lrugen; + + min_seq =3D READ_ONCE(lrugen->min_seq[file]); + return !((*token >> LRU_REFS_WIDTH) !=3D (min_seq & (EVICTION_MASK >> LRU= _REFS_WIDTH))); +} + static void lru_gen_refault(struct folio *folio, void *shadow) { int hist, tier, refs; @@ -258,23 +282,24 @@ static void lru_gen_refault(struct folio *folio, void= *shadow) int type =3D folio_is_file_lru(folio); int delta =3D folio_nr_pages(folio); - unpack_shadow(shadow, &memcg_id, &pgdat, &token, &workingset); - - if (pgdat !=3D folio_pgdat(folio)) - return; rcu_read_lock(); memcg =3D folio_memcg_rcu(folio); + + if (!lru_gen_test_recent(shadow, type, &memcg_id, &pgdat, &token, + &workingset)) + goto unlock; + if (memcg_id !=3D mem_cgroup_id(memcg)) goto unlock; + if (pgdat !=3D folio_pgdat(folio)) + goto unlock; + lruvec =3D mem_cgroup_lruvec(memcg, pgdat); lrugen =3D &lruvec->lrugen; - min_seq =3D READ_ONCE(lrugen->min_seq[type]); - if ((token >> LRU_REFS_WIDTH) !=3D (min_seq & (EVICTION_MASK >> LRU_REFS_= WIDTH))) - goto unlock; hist =3D lru_hist_from_seq(min_seq); /* see the comment in folio_lru_refs() */ @@ -306,6 +331,12 @@ static void *lru_gen_eviction(struct folio *folio) return NULL; } +static bool lru_gen_test_recent(void *shadow, int type, int *memcgid, + struct pglist_data **pgdat, unsigned long *token, bool *workingset) +{ + return true; +} + static void lru_gen_refault(struct folio *folio, void *shadow) { } @@ -373,40 +404,32 @@ 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 =3D 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 <<=3D 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 *shad= ow) */ eviction_memcg =3D mem_cgroup_from_id(memcgid); if (!mem_cgroup_disabled() && !eviction_memcg) - goto out; + return false; + eviction_lruvec =3D mem_cgroup_lruvec(eviction_memcg, pgdat); refault =3D atomic_long_read(&eviction_lruvec->nonresident_age); @@ -447,21 +471,6 @@ void workingset_refault(struct folio *folio, void *sha= dow) */ refault_distance =3D (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 =3D folio_nr_pages(folio); - memcg =3D folio_memcg(folio); - pgdat =3D folio_pgdat(folio); - lruvec =3D 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 *sha= dow) NR_INACTIVE_ANON); } } - if (refault_distance > workingset_size) + + return refault_distance <=3D 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 =3D 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 =3D folio_nr_pages(folio); + memcg =3D folio_memcg(folio); + pgdat =3D folio_pgdat(folio); + lruvec =3D 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 *shad= ow) mod_lruvec_state(lruvec, WORKINGSET_RESTORE_BASE + file, nr); } out: + mod_lruvec_state(lruvec, WORKINGSET_REFAULT_BASE + file, nr); rcu_read_unlock(); } -- 2.30.2 From nobody Thu Sep 18 14:09:52 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5087BC47089 for ; Mon, 5 Dec 2022 17:52:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232279AbiLERwg (ORCPT ); Mon, 5 Dec 2022 12:52:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44256 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232903AbiLERvt (ORCPT ); Mon, 5 Dec 2022 12:51:49 -0500 Received: from mail-pg1-x52c.google.com (mail-pg1-x52c.google.com [IPv6:2607:f8b0:4864:20::52c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B40441EC5C for ; Mon, 5 Dec 2022 09:51:47 -0800 (PST) Received: by mail-pg1-x52c.google.com with SMTP id s196so11149874pgs.3 for ; Mon, 05 Dec 2022 09:51:47 -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=U3Nw3s1OJlt+QohVcTOOKp8qc8DHu5Xw6vYT/j54rUQ=; b=PIGWqACKG6oWbEugXMS+KTR2Px2Y3qGeZuVYT1au4PLZfEuAh4gVH0bY3f2R5dFtaq 1uZxVz0caV+GcKDskviVDama9aByC8ikG5ZE5wfpd8hdrLaMVvLfB6nP9EHnC6yIcL04 NJrSpyXi0RmgnV8Miwrdv4W+roV4ITaTBS0+RO71/51ZBG5Mr9l7GnA+e9ldsYbmUbOD PCSEPnxNpStL+QByu4xU1GVCsA62d9eDstAm2kB5o6hvyjnxwwcPkqAYMP+SyB0ekqi/ owoFd4HK9Jzm1yJYjnFo767zpzz2YMWVfZBEiOweaCi5qbZBtawKw5h0Zm4tUSF8MWtD w8/w== 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=U3Nw3s1OJlt+QohVcTOOKp8qc8DHu5Xw6vYT/j54rUQ=; b=N/ubIIJ4ddSTuSqtyg62HKv/6YOuPrKRd99NNI7AgGPU6xuDakwhKln4EjZpa7H83n c1Mu7eyCD5q1qdh3K7qKFNflpsHAvXPHIE1D6evMYmamCycuoSs7lMm5A2q5K5Qlblll 8BilJHxxz6zDnQP3n4l8bQYDUfNWsVzanmAp5Io/oezBXaASOiX9w/UMUOjs57RX8owb u+JX4rVKTM2n1+VSY2a6GCPqM17NU8QP4swW6ax/yl6VO+TaT0wela8EB9NqX7G//NzB j7NeZLjUUOy/je+iMmYI+WZlPSnEtAkedfXEpUKpWyiZR/O127rALPdDGjHJKIkYb7A4 pZyQ== X-Gm-Message-State: ANoB5plb93kfhmktJVwEYtpurQohHPJl1WL8IHyJx4aCM/XnxoLbA+XB uyZLSOSY+BcDV0e6Yt16MR4= X-Google-Smtp-Source: AA0mqf4QlNt4mCMUiETIzK8/hOhXtz8rV/ETSSKnqgcWPNDEjPOgqcYrNl4P0UIqYn7Og1R+J5EKTA== X-Received: by 2002:a63:ec11:0:b0:470:5b0d:b50e with SMTP id j17-20020a63ec11000000b004705b0db50emr60322750pgh.488.1670262707128; Mon, 05 Dec 2022 09:51:47 -0800 (PST) Received: from localhost (fwdproxy-prn-118.fbsv.net. [2a03:2880:ff:76::face:b00c]) by smtp.gmail.com with ESMTPSA id e1-20020a621e01000000b0057621a437d7sm8187278pfe.116.2022.12.05.09.51.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Dec 2022 09:51:46 -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 v2 3/4] cachestat: implement cachestat syscall Date: Mon, 5 Dec 2022 09:51:39 -0800 Message-Id: <20221205175140.1543229-4-nphamcs@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20221205175140.1543229-1-nphamcs@gmail.com> References: <20221205175140.1543229-1-nphamcs@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Implement a new syscall that queries cache state of a file and summarizes the number of cached pages, number of dirty pages, number of pages marked for writeback, number of (recently) evicted pages, etc. in a given range. NAME cachestat - query the page cache status of a file. SYNOPSIS #include struct cachestat { __u64 nr_cache; __u64 nr_dirty; __u64 nr_writeback; __u64 nr_evicted; __u64 nr_recently_evicted; }; int cachestat(unsigned int fd, off_t off, size_t len, size_t cstat_size, struct cachestat *cstat); DESCRIPTION cachestat() queries the number of cached pages, number of dirty pages, number of pages marked for writeback, number of (recently) evicted pages, in the bytes range given by `off` and `len`. These values are returned in a cachestat struct, whose address is given by the `cstat` argument. The `off` and `len` arguments must be non-negative integers. If `len` > 0, the queried range is [`off`, `off` + `len`]. If `len` =3D=3D 0, we will query in the range from `off` to the end of the file. `cstat_size` allows users to obtain partial results. The syscall will copy the first `csstat_size` bytes to the specified userspace memory. `cstat_size` must be a non-negative value that is no larger than the current size of the cachestat struct. RETURN VALUE On success, cachestat returns 0. On error, -1 is returned, and errno is set to indicate the error. ERRORS EFAULT cstat points to an invalid address. EINVAL `off` or `len` is negative, or invalid `cstat_size`. EBADF invalid file descriptor. Signed-off-by: Nhat Pham --- MAINTAINERS | 7 ++ arch/x86/entry/syscalls/syscall_32.tbl | 1 + arch/x86/entry/syscalls/syscall_64.tbl | 1 + include/linux/syscalls.h | 3 + include/uapi/asm-generic/unistd.h | 5 +- include/uapi/linux/mman.h | 8 ++ kernel/sys_ni.c | 1 + mm/Makefile | 2 +- mm/cachestat.c | 115 +++++++++++++++++++++++++ 9 files changed, 141 insertions(+), 2 deletions(-) create mode 100644 mm/cachestat.c diff --git a/MAINTAINERS b/MAINTAINERS index a198da986146..baa081a1fe52 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -4552,6 +4552,13 @@ S: Supported F: Documentation/filesystems/caching/cachefiles.rst F: fs/cachefiles/ +CACHESTAT: PAGE CACHE STATS FOR A FILE +M: Nhat Pham +M: Johannes Weiner +L: linux-mm@kvack.org +S: Maintained +F: mm/cachestat.c + CADENCE MIPI-CSI2 BRIDGES M: Maxime Ripard L: linux-media@vger.kernel.org diff --git a/arch/x86/entry/syscalls/syscall_32.tbl b/arch/x86/entry/syscal= ls/syscall_32.tbl index 320480a8db4f..bc0a3c941b35 100644 --- a/arch/x86/entry/syscalls/syscall_32.tbl +++ b/arch/x86/entry/syscalls/syscall_32.tbl @@ -455,3 +455,4 @@ 448 i386 process_mrelease sys_process_mrelease 449 i386 futex_waitv sys_futex_waitv 450 i386 set_mempolicy_home_node sys_set_mempolicy_home_node +451 i386 cachestat sys_cachestat diff --git a/arch/x86/entry/syscalls/syscall_64.tbl b/arch/x86/entry/syscal= ls/syscall_64.tbl index c84d12608cd2..8eed4cdc7965 100644 --- a/arch/x86/entry/syscalls/syscall_64.tbl +++ b/arch/x86/entry/syscalls/syscall_64.tbl @@ -372,6 +372,7 @@ 448 common process_mrelease sys_process_mrelease 449 common futex_waitv sys_futex_waitv 450 common set_mempolicy_home_node sys_set_mempolicy_home_node +451 common cachestat sys_cachestat # # Due to a historical design error, certain syscalls are numbered differen= tly diff --git a/include/linux/syscalls.h b/include/linux/syscalls.h index a34b0f9a9972..55cea19b8bf6 100644 --- a/include/linux/syscalls.h +++ b/include/linux/syscalls.h @@ -72,6 +72,7 @@ struct open_how; struct mount_attr; struct landlock_ruleset_attr; enum landlock_rule_type; +struct cachestat; #include #include @@ -1056,6 +1057,8 @@ asmlinkage long sys_memfd_secret(unsigned int flags); asmlinkage long sys_set_mempolicy_home_node(unsigned long start, unsigned = long len, unsigned long home_node, unsigned long flags); +asmlinkage long sys_cachestat(unsigned int fd, off_t off, size_t len, + size_t cstat_size, struct cachestat __user *cstat); /* * Architecture-specific system calls diff --git a/include/uapi/asm-generic/unistd.h b/include/uapi/asm-generic/u= nistd.h index 45fa180cc56a..cd639fae9086 100644 --- a/include/uapi/asm-generic/unistd.h +++ b/include/uapi/asm-generic/unistd.h @@ -886,8 +886,11 @@ __SYSCALL(__NR_futex_waitv, sys_futex_waitv) #define __NR_set_mempolicy_home_node 450 __SYSCALL(__NR_set_mempolicy_home_node, sys_set_mempolicy_home_node) +#define __NR_cachestat 451 +__SYSCALL(__NR_cachestat, sys_cachestat) + #undef __NR_syscalls -#define __NR_syscalls 451 +#define __NR_syscalls 452 /* * 32 bit systems traditionally used different diff --git a/include/uapi/linux/mman.h b/include/uapi/linux/mman.h index f55bc680b5b0..ee2889e0654e 100644 --- a/include/uapi/linux/mman.h +++ b/include/uapi/linux/mman.h @@ -41,4 +41,12 @@ #define MAP_HUGE_2GB HUGETLB_FLAG_ENCODE_2GB #define MAP_HUGE_16GB HUGETLB_FLAG_ENCODE_16GB +struct cachestat { + __u64 nr_cache; + __u64 nr_dirty; + __u64 nr_writeback; + __u64 nr_evicted; + __u64 nr_recently_evicted; +}; + #endif /* _UAPI_LINUX_MMAN_H */ diff --git a/kernel/sys_ni.c b/kernel/sys_ni.c index 860b2dcf3ac4..04bfb1e4d377 100644 --- a/kernel/sys_ni.c +++ b/kernel/sys_ni.c @@ -299,6 +299,7 @@ COND_SYSCALL(set_mempolicy); COND_SYSCALL(migrate_pages); COND_SYSCALL(move_pages); COND_SYSCALL(set_mempolicy_home_node); +COND_SYSCALL(cachestat); COND_SYSCALL(perf_event_open); COND_SYSCALL(accept4); diff --git a/mm/Makefile b/mm/Makefile index 8e105e5b3e29..e71b15743ce6 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -54,7 +54,7 @@ obj-y :=3D filemap.o mempool.o oom_kill.o fadvise.o \ mm_init.o percpu.o slab_common.o \ compaction.o \ interval_tree.o list_lru.o workingset.o \ - debug.o gup.o mmap_lock.o $(mmu-y) + debug.o gup.o mmap_lock.o cachestat.o $(mmu-y) # Give 'page_alloc' its own module-parameter namespace page-alloc-y :=3D page_alloc.o diff --git a/mm/cachestat.c b/mm/cachestat.c new file mode 100644 index 000000000000..59067f26eba3 --- /dev/null +++ b/mm/cachestat.c @@ -0,0 +1,115 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +/* + * The cachestat() system call. + */ +#include +#include +#include +#include +#include +#include +#include + +#include "swap.h" + +/* + * The cachestat(4) system call. + * + * cachestat() returns the page cache status of a file in the + * bytes specified by `off` and `len`: number of cached pages, + * number of dirty pages, number of pages marked for writeback, + * number of (recently) evicted pages. + * + * `off` and `len` must be non-negative integers. If `len` > 0, + * the queried range is [`off`, `off` + `len`]. If `len` =3D=3D 0, + * we will query in the range from `off` to the end of the file. + * + * `cstat_size` allows users to obtain partial results. The syscall + * will copy the first `csstat_size` bytes to the specified userspace + * memory. It also makes the cachestat struct extensible - new fields + * can be added in the future without breaking existing usage. + * `cstat_size` must be a non-negative value that is no larger than + * the current size of the cachestat struct. + * + * Because the status of a page can change after cachestat() checks it + * but before it returns to the application, the returned values may + * contain stale information. + * + * return values: + * zero - success + * -EFAULT - cstat points to an illegal address + * -EINVAL - invalid arguments + * -EBADF - invalid file descriptor + */ +SYSCALL_DEFINE5(cachestat, unsigned int, fd, off_t, off, size_t, len, + size_t, cstat_size, struct cachestat __user *, cstat) +{ + struct fd f; + struct cachestat cs; + + memset(&cs, 0, sizeof(struct cachestat)); + + if (off < 0 || len < 0 || + cstat_size > sizeof(struct cachestat) || + cstat_size < 0) + return -EINVAL; + + if (!access_ok(cstat, sizeof(struct cachestat))) + return -EFAULT; + + f =3D fdget(fd); + if (f.file) { + struct address_space *mapping =3D f.file->f_mapping; + pgoff_t first_index =3D off >> PAGE_SHIFT; + pgoff_t last_index =3D + len =3D=3D 0 ? ULONG_MAX : (off + len - 1) >> PAGE_SHIFT; + XA_STATE(xas, &mapping->i_pages, first_index); + struct folio *folio; + + rcu_read_lock(); + xas_for_each(&xas, folio, last_index) { + if (xas_retry(&xas, folio) || !folio) + continue; + + if (xa_is_value(folio)) { + /* page is evicted */ + void *shadow =3D (void *)folio; + bool workingset; /* not used */ + + cs.nr_evicted +=3D 1; + +#if defined(CONFIG_SWAP) /* implies CONFIG_MMU */ + if (shmem_mapping(mapping)) { + /* shmem file - in swap cache */ + swp_entry_t swp =3D radix_to_swp_entry(folio); + + shadow =3D get_shadow_from_swap_cache(swp); + } +#endif + if (workingset_test_recent(shadow, true, &workingset)) + cs.nr_recently_evicted +=3D 1; + + continue; + } + + /* page is in cache */ + cs.nr_cache +=3D 1; + + if (folio_test_dirty(folio)) + cs.nr_dirty +=3D 1; + + if (folio_test_writeback(folio)) + cs.nr_writeback +=3D 1; + } + rcu_read_unlock(); + fdput(f); + + if (copy_to_user(cstat, &cs, cstat_size)) + return -EFAULT; + + return 0; + } + + return -EBADF; +} -- 2.30.2 From nobody Thu Sep 18 14:09:52 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 12F99C4321E for ; Mon, 5 Dec 2022 17:52:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232861AbiLERwr (ORCPT ); Mon, 5 Dec 2022 12:52:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44384 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232063AbiLERvu (ORCPT ); Mon, 5 Dec 2022 12:51:50 -0500 Received: from mail-pl1-x62b.google.com (mail-pl1-x62b.google.com [IPv6:2607:f8b0:4864:20::62b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0E6B01E70C for ; Mon, 5 Dec 2022 09:51:49 -0800 (PST) Received: by mail-pl1-x62b.google.com with SMTP id jl24so11522185plb.8 for ; Mon, 05 Dec 2022 09:51:49 -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=Han8q7FoqziKRrDCsnqa8WckqolCToj11E76NTsZ6WI=; b=hEPkWYZKlr36zkFuUBGQYmt3IUTD8l8z6Yjwu8TNj40vcXhAKSX3UvOySIlQHS8Fqq Z+Sz2xOdpuUi1XqQ6wf9TDuMMIJbGWAAuwatsfpzVTtec0Ub9WRKnRo82eZyJNdY/6Wo T8xRy+dlnmznXSjVCmnBCW8RPMao/atFS0S8rHLzqjheoIJF3/pAaKvdRCJWeUD3OJPh zQ3KhRaPf6IFWA/LZ3na5eLTbVahhmR1oElaoIjwGY5LAd/sxizjsCGCAmxmCIKGxw1p zxcsF86E3UsuzpemuIIkBhOtgMUsd+zoSyhbcyVvXdnxVUgWDr5ISafhrEHZsFfM3C0Y T6OA== 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=Han8q7FoqziKRrDCsnqa8WckqolCToj11E76NTsZ6WI=; b=TYS9c869+WbyEE6yr8savoEUnzOvnMG7ZrULAl1tstbgs2jn9VYs4++FmissLBhk3d XBKyw93zHlpnkmElnI7u5srhyvQVwW11addGEwIANAN1i7TIBSICoJEWlyjQPf+TRFD4 jDDwHWMOtFs2WXIHo+bUU0g4nO25X8kFK69U/Z0PhHJYJRMkqs9g5hLNXBJyc3kMJDal Eoct8JP1jI53omKKBWa6uBbB0Rgk2seR4ZCJ6oBsSCWD/9lLuocZR947uug07dQqX+Jr hChuNHUeYPwfv+Tm5vt0YhNw2/1WLwbUdikRGzwZMKyG3te05cc4HSuJR4DFS9SjMywI FfIA== X-Gm-Message-State: ANoB5pmhPWBGqYgDZjhuMPd5kacZxDmFEbMXXPhQ8u3x3VWHKJjfBTRg 9S+KMDXa8mBruIueBnV6kH8= X-Google-Smtp-Source: AA0mqf5lvaQy63EDV2t6S9BrgJR2GdaVOFzC/2UQZ+U6GNvKP7plvXWwuWiTfq/65vZrehbecl7LBQ== X-Received: by 2002:a17:902:f092:b0:189:9b43:a082 with SMTP id p18-20020a170902f09200b001899b43a082mr32749897pla.95.1670262708507; Mon, 05 Dec 2022 09:51:48 -0800 (PST) Received: from localhost (fwdproxy-prn-118.fbsv.net. [2a03:2880:ff:76::face:b00c]) by smtp.gmail.com with ESMTPSA id r19-20020a6560d3000000b0043c732e1536sm8576715pgv.45.2022.12.05.09.51.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Dec 2022 09:51:48 -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 v2 4/4] selftests: Add selftests for cachestat Date: Mon, 5 Dec 2022 09:51:40 -0800 Message-Id: <20221205175140.1543229-5-nphamcs@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20221205175140.1543229-1-nphamcs@gmail.com> References: <20221205175140.1543229-1-nphamcs@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Test cachestat on a newly created file, /dev/ files, and /proc/ files. Signed-off-by: Nhat Pham --- MAINTAINERS | 1 + tools/testing/selftests/Makefile | 1 + tools/testing/selftests/cachestat/.gitignore | 2 + tools/testing/selftests/cachestat/Makefile | 7 + .../selftests/cachestat/test_cachestat.c | 186 ++++++++++++++++++ 5 files changed, 197 insertions(+) create mode 100644 tools/testing/selftests/cachestat/.gitignore create mode 100644 tools/testing/selftests/cachestat/Makefile create mode 100644 tools/testing/selftests/cachestat/test_cachestat.c diff --git a/MAINTAINERS b/MAINTAINERS index baa081a1fe52..1b8bc6151e86 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -4558,6 +4558,7 @@ M: Johannes Weiner L: linux-mm@kvack.org S: Maintained F: mm/cachestat.c +F: tools/testing/selftests/cachestat/test_cachestat.c CADENCE MIPI-CSI2 BRIDGES M: Maxime Ripard diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Mak= efile index 0464b2c6c1e4..3cad0b38c5c2 100644 --- a/tools/testing/selftests/Makefile +++ b/tools/testing/selftests/Makefile @@ -4,6 +4,7 @@ TARGETS +=3D amd-pstate TARGETS +=3D arm64 TARGETS +=3D bpf TARGETS +=3D breakpoints +TARGETS +=3D cachestat TARGETS +=3D capabilities TARGETS +=3D cgroup TARGETS +=3D clone3 diff --git a/tools/testing/selftests/cachestat/.gitignore b/tools/testing/s= elftests/cachestat/.gitignore new file mode 100644 index 000000000000..d6c30b43a4bb --- /dev/null +++ b/tools/testing/selftests/cachestat/.gitignore @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0-only +test_cachestat diff --git a/tools/testing/selftests/cachestat/Makefile b/tools/testing/sel= ftests/cachestat/Makefile new file mode 100644 index 000000000000..04e84fd7d8ce --- /dev/null +++ b/tools/testing/selftests/cachestat/Makefile @@ -0,0 +1,7 @@ +# SPDX-License-Identifier: GPL-2.0 +TEST_GEN_PROGS :=3D test_cachestat + +CFLAGS +=3D $(KHDR_INCLUDES) +CFLAGS +=3D -Wall + +include ../lib.mk diff --git a/tools/testing/selftests/cachestat/test_cachestat.c b/tools/tes= ting/selftests/cachestat/test_cachestat.c new file mode 100644 index 000000000000..120f42ba3a5d --- /dev/null +++ b/tools/testing/selftests/cachestat/test_cachestat.c @@ -0,0 +1,186 @@ +// SPDX-License-Identifier: GPL-2.0 +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../kselftest.h" + +static const char * const dev_files[] =3D { + "/dev/zero", "/dev/null", "/dev/urandom", + "/proc/version", "/proc" +}; + +/* + * Open/create the file at filename, (optionally) write random data to it + * (exactly num_pages), then test the cachestat syscall on this file. + * + * If test_fsync =3D=3D true, fsync the file, then check the number of dir= ty + * pages. + */ +bool test_cachestat(const char *filename, bool write_random, bool create, + bool test_fsync, int num_pages, int open_flags, mode_t open_mode) +{ + int cachestat_nr =3D 451; + size_t PS =3D sysconf(_SC_PAGESIZE); + int filesize =3D num_pages * PS; + bool ret =3D true; + int random_fd; + long syscall_ret; + struct cachestat cs; + + int fd =3D open(filename, open_flags, open_mode); + + if (fd =3D=3D -1) { + ksft_print_msg("Unable to create/open file.\n"); + goto out; + } else { + ksft_print_msg("Create/open %s\n", filename); + } + + if (write_random) { + char data[filesize]; + + random_fd =3D open("/dev/urandom", O_RDONLY); + + if (random_fd < 0) { + ksft_print_msg("Unable to access urandom.\n"); + ret =3D false; + goto out1; + } else { + int remained =3D filesize; + char *cursor =3D data; + + while (remained) { + ssize_t read_len =3D read(random_fd, cursor, remained); + + if (read_len <=3D 0) { + ksft_print_msg("Unable to read from urandom.\n"); + ret =3D false; + goto out2; + } + + remained -=3D read_len; + cursor +=3D read_len; + } + + /* write random data to fd */ + remained =3D filesize; + cursor =3D data; + while (remained) { + ssize_t write_len =3D write(fd, cursor, remained); + + if (write_len <=3D 0) { + ksft_print_msg("Unable write random data to file.\n"); + ret =3D false; + goto out2; + } + + remained -=3D write_len; + cursor +=3D write_len; + } + } + } + + syscall_ret =3D syscall(cachestat_nr, fd, 0, filesize, + sizeof(struct cachestat), &cs); + + ksft_print_msg("Cachestat call returned %ld\n", syscall_ret); + + if (syscall_ret) { + ksft_print_msg("Cachestat returned non-zero.\n"); + ret =3D false; + + if (write_random) + goto out2; + else + goto out1; + + } else { + ksft_print_msg( + "Using cachestat: Cached: %lu, Dirty: %lu, Writeback: %lu, Evicted: %lu= , Recently Evicted: %lu\n", + cs.nr_cache, cs.nr_dirty, cs.nr_writeback, + cs.nr_evicted, cs.nr_recently_evicted); + + if (write_random) { + if (cs.nr_cache + cs.nr_evicted !=3D num_pages) { + ksft_print_msg( + "Total number of cached and evicted pages is off.\n"); + ret =3D false; + } + } + } + + if (test_fsync) { + if (fsync(fd)) { + ksft_print_msg("fsync fails.\n"); + ret =3D false; + } else { + syscall_ret =3D syscall(cachestat_nr, fd, 0, filesize, + sizeof(struct cachestat), &cs); + + ksft_print_msg("Cachestat call (after fsync) returned %ld\n", + syscall_ret); + + if (!syscall_ret) { + ksft_print_msg( + "Using cachestat: Cached: %lu, Dirty: %lu, Writeback: %lu, Evicted: %= lu, Recently Evicted: %lu\n", + cs.nr_cache, cs.nr_dirty, cs.nr_writeback, + cs.nr_evicted, cs.nr_recently_evicted); + + if (cs.nr_dirty) { + ret =3D false; + ksft_print_msg( + "Number of dirty should be zero after fsync.\n"); + } + } else { + ksft_print_msg("Cachestat (after fsync) returned non-zero.\n"); + ret =3D false; + + if (write_random) + goto out2; + else + goto out1; + } + } + } + +out2: + if (write_random) + close(random_fd); +out1: + close(fd); + + if (create) + remove(filename); +out: + return ret; +} + +int main(void) +{ + for (int i =3D 0; i < 5; i++) { + const char *dev_filename =3D dev_files[i]; + + if (test_cachestat(dev_filename, false, false, false, + 4, O_RDONLY, 0400)) + ksft_test_result_pass("cachestat works with %s\n", dev_filename); + else + ksft_test_result_fail("cachestat fails with %s\n", dev_filename); + } + + if (test_cachestat("tmpfilecachestat", true, true, + true, 4, O_CREAT | O_RDWR, 0400 | 0600)) + ksft_test_result_pass("cachestat works with a normal file\n"); + else + ksft_test_result_fail("cachestat fails with normal file\n"); + + return 0; +} -- 2.30.2