From patchwork Tue Nov 15 18:28:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nhat Pham X-Patchwork-Id: 13044070 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 32758C433FE for ; Tue, 15 Nov 2022 18:29:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B83726B0072; Tue, 15 Nov 2022 13:29:06 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B33B56B0073; Tue, 15 Nov 2022 13:29:06 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9FB586B0074; Tue, 15 Nov 2022 13:29:06 -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 90C256B0072 for ; Tue, 15 Nov 2022 13:29:06 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 3C58B140A09 for ; Tue, 15 Nov 2022 18:29:06 +0000 (UTC) X-FDA: 80136513492.25.EE555E8 Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) by imf10.hostedemail.com (Postfix) with ESMTP id CFC19C000C for ; Tue, 15 Nov 2022 18:29:05 +0000 (UTC) Received: by mail-pf1-f178.google.com with SMTP id 130so14864850pfu.8 for ; Tue, 15 Nov 2022 10:29:05 -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=NPDHr0QlHVIUIhIrDfHxvLDIuBOtKZfo7tv9Z1S0sFSK9TGsnpiguY2DVaj8KQqkUg 2SugxfLILvY2yZb9mSRU2q3++D6K9Vj+iOyidmF/ozO/p/7XOGWmFRfizSWm5iaLk5hj il4Q4TvN2HUyX2sk4tSfr2n2+tzXd77V5UybYcVVdBEnLxiMpx20HM6oe4QsCFyHtEs2 LJ3vSboCcqML9M7WDu6BnBSGDB+A5Hgh9HctMZlRn/EuhBDkfp2Exqkb6OqkY16lFgjL mqYbnVzmCbSOyK+6Jgh/buBRjxAFZ5UldLk6C7sULrRpi2KwbgfgjYgFwkYbz8uiAGV1 sx/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=npFjL6Dds3DKnQo07iUTlbqXMaucW9/yvERNl8ZNQRo=; b=T5IzUIXSCsAppRO4YyZ9uyMk1+i4TpEfNbNvlBi4dDUtbVeGNkli0FpaiGZtzOwIEd 489zmA/YUY9BGEcuS/gdj/gawdP+psOitXakcgKPdedRkywHBFpNNdupFNU18g4U7XJ2 jLd92mU+UekJ0cWU1WmJ9mN2BTtk6DZf3rT4LTmEseTpA5YNRJCKNa1dD/U8fOes942a +iad3xpLtMN1GGJDotsn53Q7LUYsbeiH/+4ADSQF05bTSmRFjizDBI8jriZDrY5/ogwk h3YZRx2DqtXsVJlmr4vaU3uSL4GILlgx66do4hkuoaxuhvpP1CvIn1hvxHtUULDsJ7wn m7Gw== X-Gm-Message-State: ANoB5pkQE7MaOq3PmqqqxKtUVOjxA/9XfNDZkBP4EkF2/PDIRX07SGU8 lpB5vWQ6UMuxkRDLsRC4ageWpfcIcbf02Q== X-Google-Smtp-Source: AA0mqf7wqzv071P0x/mRXjpwael9ewocrXAjhS7lO6DJTDtlWHz7zdSkAz307rNNRRW624ZDI3KXTA== X-Received: by 2002:a63:560c:0:b0:476:9983:b4b5 with SMTP id k12-20020a63560c000000b004769983b4b5mr6888328pgb.516.1668536944373; Tue, 15 Nov 2022 10:29:04 -0800 (PST) Received: from localhost (fwdproxy-prn-116.fbsv.net. [2a03:2880:ff:74::face:b00c]) by smtp.gmail.com with ESMTPSA id k15-20020aa7972f000000b0056bbba4302dsm9034046pfg.119.2022.11.15.10.29.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Nov 2022 10:29:03 -0800 (PST) From: Nhat Pham To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: hannes@cmpxchg.org Subject: [PATCH 1/4] workingset: fix confusion around eviction vs refault container Date: Tue, 15 Nov 2022 10:28:58 -0800 Message-Id: <20221115182901.2755368-2-nphamcs@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20221115182901.2755368-1-nphamcs@gmail.com> References: <20221115182901.2755368-1-nphamcs@gmail.com> MIME-Version: 1.0 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1668536945; a=rsa-sha256; cv=none; b=J7U8ySL7OkXhO2Pn8Tn3Nopy3LISkKjNyqqo6ald/1QsBI146xy6ZQcpSpT1ydSzQVlf0c wODvb02V5edXj6+bf6DJh6lD3c6YRyoki6bVxCuEvTfMs60skEEFnLfV9/NFKtkPOZv4BM Wc1kbKVsDc/XhucngEBpXTgSKdEFTH0= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=NPDHr0Ql; spf=pass (imf10.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.210.178 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=1668536945; 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=npFjL6Dds3DKnQo07iUTlbqXMaucW9/yvERNl8ZNQRo=; b=wFYfV25OtHF/UXahfCjP5f4b7pO9bRROOvU80PB1/v4iZyd82TckdFOyKLaCECRawCF/gH uKdSzroWtVja/Y6QMiWIw/Ct+sh5Spnr0DLAhE1sKj/Jb10GyUzKye5eJ8mJMVGmYEXqan wbepIya+cDLNIW6Zj+c/aAZtD1usf7Q= X-Stat-Signature: 4croqodadewrydmuu9ofj1edtin5nkpk X-Rspamd-Queue-Id: CFC19C000C Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=NPDHr0Ql; spf=pass (imf10.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.210.178 as permitted sender) smtp.mailfrom=nphamcs@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-Rspam-User: X-Rspamd-Server: rspam12 X-HE-Tag: 1668536945-823009 X-Bogosity: Ham, tests=bogofilter, spamicity=0.006472, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 *shadow) */ 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); @@ -474,7 +475,7 @@ void workingset_refault(struct folio *folio, void *shadow) workingset_size += 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 += lruvec_page_state(eviction_lruvec, NR_ACTIVE_ANON); if (file) { From patchwork Tue Nov 15 18:28:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nhat Pham X-Patchwork-Id: 13044071 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 93452C43217 for ; Tue, 15 Nov 2022 18:29:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D58C66B0073; Tue, 15 Nov 2022 13:29:07 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CE5966B0074; Tue, 15 Nov 2022 13:29:07 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B0CC78E0001; Tue, 15 Nov 2022 13:29:07 -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 A403B6B0073 for ; Tue, 15 Nov 2022 13:29:07 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 7FFA9AB68F for ; Tue, 15 Nov 2022 18:29:07 +0000 (UTC) X-FDA: 80136513534.11.8BB5C0B Received: from mail-pj1-f47.google.com (mail-pj1-f47.google.com [209.85.216.47]) by imf02.hostedemail.com (Postfix) with ESMTP id 178DD80011 for ; Tue, 15 Nov 2022 18:29:06 +0000 (UTC) Received: by mail-pj1-f47.google.com with SMTP id v4-20020a17090a088400b00212cb0ed97eso14604913pjc.5 for ; Tue, 15 Nov 2022 10:29:06 -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=XlpGuGY3sJe9DLKH41vLTGd0LKIzHOuJ8+yi4Jjp058=; b=BBr8ahhuM2MCFKMZ/s05AiGPyeVkO2G9z/hbSkzjX0lY2G19nKIRAyPv4LqvFGtRk9 R/WM5NlfWEmS9TsTF3hsJV3HDoHwfpujZtu2vUrwPpgvEIK5BZ0G1j3cyWbnL1r2MFyz 8MbBg0kNP/rP4TSeJTRUMU2aPplm/armvyU4+xP6PDyGvSjgWIiD1K866M+41yLSVI0a +pZl3mCaixU/HKYf3lfmmlU6uq1HHpJyv/p2iuhS+flKIkzR7Ga0FDiXHMovKRhHeOtp MSQIVWlms2IcBe2h73fwCwkXDSDzXrO5UbL7MUmk49FBcfVCFk+7CiHIlWv+oKhEVENS DSGQ== 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=XlpGuGY3sJe9DLKH41vLTGd0LKIzHOuJ8+yi4Jjp058=; b=dt/8+35X0Su4wd0xz//fYfgFfuki1H/COjJu3ScTTbxAsssoUy51Z4Rd0MOonJXKDd 8ICfj9bGXVd7NzXJW2tg2UtaQV5oL0f5rVjrXsaBDhyUADgQpUlCyPHtbkXbDa+NLQZs 6nFQ+g1vDWJz9Jeqjue5Rp/RERMULOX463gL1DWAFxYFsTzbPu9OMhetzs/VxpTD2NxU vlZ8woPu5eqxQUmOfaF6B7NASFycdCEyKJoGYssvXuxc+def1VaqmV49QFTFYrtEe2Bf I7VoldSrKRLsfHpqydTcsJOuLAddap+PQGddYFDhVv8Ha9fMASMfu1h1b0edmzXqIKrM bKiQ== X-Gm-Message-State: ANoB5pnrH5LRzcBnQutWRo7UCGR3R2AiW03Jik0cMCXrEuCHegpRBk28 2kMUnl26sFVLi8Egc8Yp2CiQsjQrgzvpXA== X-Google-Smtp-Source: AA0mqf5IfXE8x2kz25f2TNlUhdzOFwCYbVwyJMG2ZT25VVWRdudCRU17PIpKVGJDsyeTpvz7/Udf6g== X-Received: by 2002:a17:90b:268f:b0:212:ce2d:9fd7 with SMTP id pl15-20020a17090b268f00b00212ce2d9fd7mr3529401pjb.157.1668536945693; Tue, 15 Nov 2022 10:29:05 -0800 (PST) Received: from localhost (fwdproxy-prn-014.fbsv.net. [2a03:2880:ff:e::face:b00c]) by smtp.gmail.com with ESMTPSA id b24-20020aa79518000000b0056afd55722asm9062101pfp.153.2022.11.15.10.29.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Nov 2022 10:29:05 -0800 (PST) From: Nhat Pham To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: hannes@cmpxchg.org Subject: [PATCH 2/4] workingset: refactor LRU refault to expose refault recency check Date: Tue, 15 Nov 2022 10:28:59 -0800 Message-Id: <20221115182901.2755368-3-nphamcs@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20221115182901.2755368-1-nphamcs@gmail.com> References: <20221115182901.2755368-1-nphamcs@gmail.com> MIME-Version: 1.0 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1668536947; 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=XlpGuGY3sJe9DLKH41vLTGd0LKIzHOuJ8+yi4Jjp058=; b=ql5Oj05fDP7Opm6PmpUy7xICzNNObkP0stZNIeO0NBNDJF/2+K2SH7FG8LuQCvvsmW1yP3 eiTXn81OpyBVTVevYjIFk6OXs34WK6fidAjdPoKvWgf0KUxybAwaa2evVeCyBbtCSO2phR T6xioY+CSr7USx1eDpCK5ViTVPZXcDk= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=BBr8ahhu; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf02.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.216.47 as permitted sender) smtp.mailfrom=nphamcs@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1668536947; a=rsa-sha256; cv=none; b=5IKe1SgaX1527ydxUBEmNvmtMnzVFRDdNkWjgDvlTjQ6YE2v8IgmQDZx3EbHqwt+y791DL lm5ARnXqtQGQfQOGRGOaVA5cUMD0XdrOkM02ZGfjtu2+D668fjTNX4S/Bb4ZV5nTuZayOA 43y0bkosCSO9EdsgqbEbRaStSQSj1LA= X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 178DD80011 X-Rspam-User: Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=BBr8ahhu; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf02.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.216.47 as permitted sender) smtp.mailfrom=nphamcs@gmail.com X-Stat-Signature: rhdi87ktj7sp1c8o7b6mos9zsdgc5161 X-HE-Tag: 1668536946-560896 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000813, 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 | 141 +++++++++++++++++++++++++++++-------------- 2 files changed, 98 insertions(+), 44 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..51ed8c836467 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,24 @@ 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(); memcg = folio_memcg_rcu(folio); + + if (!lru_gen_test_recent(shadow, type, &memcg_id, &pgdat, &token, + &workingset)) + goto unlock; + if (memcg_id != mem_cgroup_id(memcg)) goto unlock; + if (pgdat != folio_pgdat(folio)) + goto unlock; + 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 +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) { } @@ -374,39 +405,31 @@ void *workingset_eviction(struct folio *folio, struct mem_cgroup *target_memcg) } /** - * 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 +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(); } From patchwork Tue Nov 15 18:29:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nhat Pham X-Patchwork-Id: 13044072 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 1B00BC433FE for ; Tue, 15 Nov 2022 18:29:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8ED3A6B0074; Tue, 15 Nov 2022 13:29:09 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 89E3F6B0075; Tue, 15 Nov 2022 13:29:09 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 73C5C8E0001; Tue, 15 Nov 2022 13:29:09 -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 5C2086B0074 for ; Tue, 15 Nov 2022 13:29:09 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 3323E80524 for ; Tue, 15 Nov 2022 18:29:09 +0000 (UTC) X-FDA: 80136513618.28.85550BA Received: from mail-pg1-f170.google.com (mail-pg1-f170.google.com [209.85.215.170]) by imf05.hostedemail.com (Postfix) with ESMTP id B5F3C100002 for ; Tue, 15 Nov 2022 18:29:08 +0000 (UTC) Received: by mail-pg1-f170.google.com with SMTP id q1so14026578pgl.11 for ; Tue, 15 Nov 2022 10:29:08 -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=R+CgD6b3VuRjXvN+H9oTC8+sOdeozxCPLL1LFRJAE7c=; b=DcMxx7DRBkE7cfl2G0MrzZbevyc+hmDxLJ95GRKE3hesCDxUvL18vQyoEMB0t27Snp SYqi/0I0t1zns6q62mPtM9geuwYeZv0m+pcz2Qt537doPYDH2nlh53hOT27pPwvjBlSG UDe3T+56318o8ql2nuTQjslk5K/vWpRj15rrsHPd6gf/b6wHktO7kLZ66fmw3G93Bvmn FJwSHo1EEO8Py5BA6fW8Q7nbKwkKpM8z5RM0fpx6vy6sTuDcdAIwqOx4EQALEhCI1Lac gVCt10HnuJ50rr0F4152yj7SoUs9oMeZcoFv64jCKQ4j+UeT8k/8aPF2QFC6+EVySP5B urTA== 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=R+CgD6b3VuRjXvN+H9oTC8+sOdeozxCPLL1LFRJAE7c=; b=fxX9VI4hrd2umgrU7pbxJUqOFTmCBA7KYUvZBbkqy5161PkJxMjMzNflHilotbVVCh 123O+fKcZwdYLnKPKlv4Hqi7zl3FdaOXM5DGFFSCHX69/0wxDcEqHIdoS64e03zOx4sk PmpJ3KGtrScUFYxu/1RazOrnbX8IHJrcmbCsyU719EG/xrPI4mkBXXKJhNte+aRaOYuY hr7rcDfjWvcg56ZGCihsUZ9k8L+v/bxIdCs8AwZ0u8q/UPZNzZBp0eMbQy4jIsCQllzE HZYbSHfqOEZeGmkZyZswugyG3rlLWSCzxqMfCatrN/I2+3zvhohDWde0BQrwkVG8Zz88 N2Og== X-Gm-Message-State: ANoB5pnkGAqE1j9UPUoB7vvxq2RGp/ng5guaolKtYinhBut/kgZNqUwF YX2mGjKUvN8JpWA/oIGBiENtjWIcqooT+w== X-Google-Smtp-Source: AA0mqf7i6JW5fSPGB3a/irLrZrIpTzGyhKZQPxL70HKFZLgQRVhdD/4Gk6F7Fboe6pSAjmAZjP5yLA== X-Received: by 2002:a63:f006:0:b0:46f:d9f:2788 with SMTP id k6-20020a63f006000000b0046f0d9f2788mr17098327pgh.195.1668536947197; Tue, 15 Nov 2022 10:29:07 -0800 (PST) Received: from localhost (fwdproxy-prn-001.fbsv.net. [2a03:2880:ff:1::face:b00c]) by smtp.gmail.com with ESMTPSA id ja13-20020a170902efcd00b0017f7c4e260fsm10233512plb.150.2022.11.15.10.29.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Nov 2022 10:29:06 -0800 (PST) From: Nhat Pham To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: hannes@cmpxchg.org Subject: [PATCH 3/4] cachestat: implement cachestat syscall Date: Tue, 15 Nov 2022 10:29:00 -0800 Message-Id: <20221115182901.2755368-4-nphamcs@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20221115182901.2755368-1-nphamcs@gmail.com> References: <20221115182901.2755368-1-nphamcs@gmail.com> MIME-Version: 1.0 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1668536948; a=rsa-sha256; cv=none; b=R8XwYqtTM6xkwOCyae92Ek5BTA4WCOgrxyKNyk2mW3ORwIPCJxQtvHLvZp02IX3qLWPQ5F 0yeDu+KvYc+izpwlUfwZJ0CV6pJVXSHwz5R/Mbjp6ueeZIJESDyuCvBhtgLnUoDqKy3rT/ r7hQW8YB8Pp3oING3ox8wLRP3IzV3xI= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=DcMxx7DR; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf05.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.215.170 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=1668536948; 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=R+CgD6b3VuRjXvN+H9oTC8+sOdeozxCPLL1LFRJAE7c=; b=RSDGdoqaUOAHSw14tig5N6x942T0V6MW4BAicOMz6JDOD3/QXODE0QIsPxaSE0QLlbmeov L7yifLwul1WJ7GM82rEeQrP47fiEBzykvgaBn74GnIHvFnUzP4vnVH2981cTG+M30huXl5 F1zuTRSaUgFnACfgY/GxMiqj+68untI= X-Rspamd-Queue-Id: B5F3C100002 Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=DcMxx7DR; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf05.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.215.170 as permitted sender) smtp.mailfrom=nphamcs@gmail.com X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 417p41miozzt41eq4jbch3te1awj3u4j X-HE-Tag: 1668536948-491775 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 { unsigned long nr_cache; unsigned long nr_dirty; unsigned long nr_writeback; unsigned long nr_evicted; unsigned long nr_recently_evicted; }; int cachestat(unsigned int fd, off_t off, size_t len, 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` argument must be a non-negative integers, If `off` + `len` >= `off`, the queried range is [`off`, `off` + `len`]. Otherwise, we will query in the range from `off` to the end of the file. 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` is negative. 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 | 2 + include/uapi/asm-generic/unistd.h | 5 +- include/uapi/linux/mman.h | 8 ++ kernel/sys_ni.c | 1 + mm/Makefile | 2 +- mm/cachestat.c | 109 +++++++++++++++++++++++++ 9 files changed, 134 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/syscalls/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/syscalls/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 differently diff --git a/include/linux/syscalls.h b/include/linux/syscalls.h index a34b0f9a9972..59e4d56225ae 100644 --- a/include/linux/syscalls.h +++ b/include/linux/syscalls.h @@ -1056,6 +1056,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, + struct cachestat __user *ret_cstat); /* * Architecture-specific system calls diff --git a/include/uapi/asm-generic/unistd.h b/include/uapi/asm-generic/unistd.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..8690c82acd02 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 { + unsigned long nr_cache; + unsigned long nr_dirty; + unsigned long nr_writeback; + unsigned long nr_evicted; + unsigned long 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 := 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 := page_alloc.o diff --git a/mm/cachestat.c b/mm/cachestat.c new file mode 100644 index 000000000000..193151cb0767 --- /dev/null +++ b/mm/cachestat.c @@ -0,0 +1,109 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +/* + * The cachestat() system call. + */ +#include +#include +#include +#include +#include +#include +#include + +#include "swap.h" + +/* + * The cachestat(3) 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. + * + * If `off` + `len` >= `off`, the queried range is [`off`, `off` + `len`]. + * Otherwise, we will query in the range from `off` to the end of the file. + * + * 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_DEFINE4(cachestat, unsigned int, fd, off_t, off, size_t, len, + struct cachestat __user *, cstat) +{ + struct fd f; + struct cachestat cs; + + memset(&cs, 0, sizeof(struct cachestat)); + + if (off < 0) + return -EINVAL; + + if (!access_ok(cstat, sizeof(struct cachestat))) + return -EFAULT; + + f = fdget(fd); + if (f.file) { + struct address_space *mapping = f.file->f_mapping; + pgoff_t first_index = off >> PAGE_SHIFT; + XA_STATE(xas, &mapping->i_pages, first_index); + struct folio *folio; + pgoff_t last_index = (off + len - 1) >> PAGE_SHIFT; + + if (last_index < first_index) + last_index = ULONG_MAX; + + 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; + bool workingset; /* not used */ + + cs.nr_evicted += 1; + + if (shmem_mapping(mapping)) { + /* shmem file - in swap cache */ + swp_entry_t swp = radix_to_swp_entry(folio); + + shadow = get_shadow_from_swap_cache(swp); + } else { + /* in page cache */ + shadow = (void *) folio; + } + + if (workingset_test_recent(shadow, true, &workingset)) + cs.nr_recently_evicted += 1; + + continue; + } + + /* page is in cache */ + cs.nr_cache += 1; + + if (folio_test_dirty(folio)) + cs.nr_dirty += 1; + + if (folio_test_writeback(folio)) + cs.nr_writeback += 1; + } + rcu_read_unlock(); + fdput(f); + + if (copy_to_user(cstat, &cs, sizeof(struct cachestat))) + return -EFAULT; + + return 0; + } + + return -EBADF; +} From patchwork Tue Nov 15 18:29:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nhat Pham X-Patchwork-Id: 13044073 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 8C601C4332F for ; Tue, 15 Nov 2022 18:29:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 82B126B0075; Tue, 15 Nov 2022 13:29:10 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7B1FB6B007B; Tue, 15 Nov 2022 13:29:10 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5DF0E8E0001; Tue, 15 Nov 2022 13:29:10 -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 4C98F6B0075 for ; Tue, 15 Nov 2022 13:29:10 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 25250AB3C6 for ; Tue, 15 Nov 2022 18:29:10 +0000 (UTC) X-FDA: 80136513660.20.DFB803D Received: from mail-pj1-f51.google.com (mail-pj1-f51.google.com [209.85.216.51]) by imf10.hostedemail.com (Postfix) with ESMTP id C88E5C0004 for ; Tue, 15 Nov 2022 18:29:09 +0000 (UTC) Received: by mail-pj1-f51.google.com with SMTP id c15-20020a17090a1d0f00b0021365864446so14618126pjd.4 for ; Tue, 15 Nov 2022 10:29:09 -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=JBJtHo7HE+ehk9NsJj/gfUpQS38HB83uuNN8e7+fjB0=; b=kAbfOcrnzxhw+r4AIOfRwZwegjd1FrFbI4FZPDZBetL6kgnKquu896dabSZMqHFUs4 G21hOllugMuxrK/16H0MyDZA7JhLltQ+IU/ROTK+yPrJKL9wUBX9UniSB1Utyi5xClvS OqHk9YvuWqB9PvI0ixu7waHEUn2KDmc8qvyJ0SKSpwoVrPnZNVIKETm5Cufwv8YoWv/N ZFE4GnqrS5NgqMCZ/3EGfgKEaq/y7Qb4UWLTYHMsUXo0+ETUM1Rm0o5mfWibqEiIoSwJ Pf5A5lQtnfN6Td3CY1Kjp8cJhCDZ+xkXHeYEaTj+48v1b7AGmRyiPVF/KCSFkKE8CM8o 0G9g== 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=JBJtHo7HE+ehk9NsJj/gfUpQS38HB83uuNN8e7+fjB0=; b=ufvTQxerxFpzKwLvGpWRfiDXZjVZUgKzGAmQFD8vaf1OgDu9AQ6UXTnxHhbaBdT2IX zavJ3A6p4rZ+VcA/86d258TUI2AVYVTVpsaSi46pEHZ0v/B22yYsMiTYEfUoUY2KNHSD vp/a5tTXY4n3y+Je2NaDeufhHRvjEJ/E2ArlJ9HTj3mIivL8oGje8lABa5sYM9tI2tJR O2LDGigGsNRevhy1xObDMwz+1cCJK1I4ydBS3315k0UBd0jRrquh7eNby5LTKaGnPSqi ZhJYgd/d3iQCF2mnVXzp07nFv+0y2xHW6xDdxwU3M3xtvDqLBgtg8RnRfdL9rufrZZlb YSZg== X-Gm-Message-State: ANoB5plGK5lpwui7wb1cyFGe8+c0AbHrS4mwB2hvf6djdUQnEscc61jp Ign7o7FdqxTquTNhejcuwIImcU6oYGDRew== X-Google-Smtp-Source: AA0mqf7wOcX3dla9QmXMdO0tsFp83EsguQYZiSrY09fm8FBUoXQKVJo0tNzsgtCqeVwT0e98dAVLCA== X-Received: by 2002:a17:90a:8c17:b0:209:46fe:871b with SMTP id a23-20020a17090a8c1700b0020946fe871bmr1754897pjo.163.1668536948456; Tue, 15 Nov 2022 10:29:08 -0800 (PST) Received: from localhost (fwdproxy-prn-116.fbsv.net. [2a03:2880:ff:74::face:b00c]) by smtp.gmail.com with ESMTPSA id y16-20020a17090322d000b00186f0f59c85sm10349827plg.235.2022.11.15.10.29.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Nov 2022 10:29:08 -0800 (PST) From: Nhat Pham To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: hannes@cmpxchg.org Subject: [PATCH 4/4] selftests: Add selftests for cachestat Date: Tue, 15 Nov 2022 10:29:01 -0800 Message-Id: <20221115182901.2755368-5-nphamcs@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20221115182901.2755368-1-nphamcs@gmail.com> References: <20221115182901.2755368-1-nphamcs@gmail.com> MIME-Version: 1.0 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1668536949; a=rsa-sha256; cv=none; b=XQbkgXG6Dax46u+BDmWY3FcFUFbjYtDXkHVCMlgNljM+EWQhwP+r5DlaUVuV76kAGhDSUT 3dowfS1XGqglxq357YoOxLkY919MC/hJwF3xXVS4c6AKj8wdmhGPGmegerg/uo62+2p3gD k0/ilR2h9T3ghZOWY9rKIv5VWsQga8w= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=kAbfOcrn; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf10.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.216.51 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=1668536949; 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=JBJtHo7HE+ehk9NsJj/gfUpQS38HB83uuNN8e7+fjB0=; b=QZtSniH+m5mJORItBLZJTV0pgP/zmDy/LaHQvn6EJJL2nww418YLa6pg/19WUq5cLlzM9Y DRoy6RLJu6KzlELMomj4OmNc7vVA5eCNVaxg67O8H675XW7qSHR2uuwfTH0bAsz+Vjj6F2 PyvJ9EGUMCJBsnabguQ2DtQ6ZNk+mLM= X-Rspamd-Queue-Id: C88E5C0004 X-Rspam-User: Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=kAbfOcrn; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf10.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.216.51 as permitted sender) smtp.mailfrom=nphamcs@gmail.com X-Rspamd-Server: rspam06 X-Stat-Signature: 343cyjewqyndur6rdfw45gpnxqqwrs69 X-HE-Tag: 1668536949-69315 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000134, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 | 9 + .../selftests/cachestat/test_cachestat.c | 184 ++++++++++++++++++ 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/Makefile index 0464b2c6c1e4..3cad0b38c5c2 100644 --- a/tools/testing/selftests/Makefile +++ b/tools/testing/selftests/Makefile @@ -4,6 +4,7 @@ TARGETS += amd-pstate TARGETS += arm64 TARGETS += bpf TARGETS += breakpoints +TARGETS += cachestat TARGETS += capabilities TARGETS += cgroup TARGETS += clone3 diff --git a/tools/testing/selftests/cachestat/.gitignore b/tools/testing/selftests/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/selftests/cachestat/Makefile new file mode 100644 index 000000000000..41244c73d5b0 --- /dev/null +++ b/tools/testing/selftests/cachestat/Makefile @@ -0,0 +1,9 @@ +# SPDX-License-Identifier: GPL-2.0 +TEST_GEN_PROGS := test_cachestat + +top_srcdir = $(realpath ../../../../) + +CFLAGS += -I$(top_srcdir)/usr/include/ +CFLAGS += -Wall + +include ../lib.mk diff --git a/tools/testing/selftests/cachestat/test_cachestat.c b/tools/testing/selftests/cachestat/test_cachestat.c new file mode 100644 index 000000000000..33f29b744509 --- /dev/null +++ b/tools/testing/selftests/cachestat/test_cachestat.c @@ -0,0 +1,184 @@ +// 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[] = { + "/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 == true, fsync the file, then check the number of dirty + * 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 = 451; + size_t PS = sysconf(_SC_PAGESIZE); + int filesize = num_pages * PS; + bool ret = true; + int random_fd; + long syscall_ret; + struct cachestat cs; + + int fd = open(filename, open_flags, open_mode); + + if (fd == -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 = open("/dev/urandom", O_RDONLY); + + if (random_fd < 0) { + ksft_print_msg("Unable to access urandom.\n"); + ret = false; + goto out1; + } else { + int remained = filesize; + char *cursor = data; + + while (remained) { + ssize_t read_len = read(random_fd, cursor, remained); + + if (read_len <= 0) { + ksft_print_msg("Unable to read from urandom.\n"); + ret = false; + goto out2; + } + + remained -= read_len; + cursor += read_len; + } + + /* write random data to fd */ + remained = filesize; + cursor = data; + while (remained) { + ssize_t write_len = write(fd, cursor, remained); + + if (write_len <= 0) { + ksft_print_msg("Unable write random data to file.\n"); + ret = false; + goto out2; + } + + remained -= write_len; + cursor += write_len; + } + } + } + + syscall_ret = syscall(cachestat_nr, fd, 0, filesize, &cs); + + ksft_print_msg("Cachestat call returned %ld\n", syscall_ret); + + if (syscall_ret) { + ksft_print_msg("Cachestat returned non-zero.\n"); + ret = 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 != num_pages) { + ksft_print_msg( + "Total number of cached and evicted pages is off.\n"); + ret = false; + } + } + } + + if (test_fsync) { + if (fsync(fd)) { + ksft_print_msg("fsync fails.\n"); + ret = false; + } else { + syscall_ret = syscall(cachestat_nr, fd, 0, filesize, &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 = 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 = 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 = 0; i < 5; i++) { + const char *dev_filename = 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; +}