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: 20501 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2882794wru; Tue, 15 Nov 2022 10:34:41 -0800 (PST) X-Google-Smtp-Source: AA0mqf60MzeS7/Ny+GhsMoWp19kQa74zoaQV07z/FL+iplFfyPznrh1MKIkcWRWY6JLn/2FMnjvX X-Received: by 2002:a63:5c46:0:b0:43c:a5cb:5d1b with SMTP id n6-20020a635c46000000b0043ca5cb5d1bmr17606026pgm.134.1668537281699; Tue, 15 Nov 2022 10:34:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668537281; cv=none; d=google.com; s=arc-20160816; b=Vu1KMaukVSRfDDZnmuvsgLX2kewfE97H2S0zV029mZYjZJcD0HR7krQoyayESFXAwj WW/5hGBecMqC07uS7Iyo4JZAdH1cuw6O9Lr/1c+GGxgHYNfqIDxZiQtInCgFA0UKgEHV 9Cts2KQENb9bZt+I0s4gLITXN3q6A3eeWiXGPYpKofxHIrOESUcMvnq3YOb2i0/Z1lmU f95fAzqvxD7EZrF9sxMjhPA8TWeBnindNvVtVq9GP7tUuiPPNxMM/gcA7vAsmqCKrhU3 mn3vuDTFuNzurbj54OsEzFTN9kgelvUJSep3kTctxxUnSlSehS1wCoae2Bpq1jNVv9KT iBSA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=npFjL6Dds3DKnQo07iUTlbqXMaucW9/yvERNl8ZNQRo=; b=FEPuNDVHHeAnD2NhUWxV4mytOjA2Ou+PfQwa2swjiSMqLW3bGMAHAy5ZIpJ++gJz6j ikjpEg3I/mGXkBhouh4hY1gGmnhi6Ed+XLiK6fruiM8p3oP7jFeQAZ9xlyCsLli0/u93 xJBFcwUOMSDprft6ywk+XlO61vuUH6vsU/pvzSaK9ZZf0S2DqOPnNyq8l9+hNMeGY6Xe S0tFaqTAFReNogUnodtjz7jvAHRw3Cx9r2QxgQ2I/O2hkSiUt3CZIO3dD4IWLdPXFq5c 3Qp1xZp0+SEsojZ7+nSEe0hqHqmuS/+oRGThgo0aUNtDMqd6D+0rwZwf5Qp49sKy6RQ6 vjtA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=NPDHr0Ql; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t18-20020a63dd12000000b004405047579csi12723471pgg.540.2022.11.15.10.34.27; Tue, 15 Nov 2022 10:34:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=NPDHr0Ql; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231540AbiKOS3b (ORCPT + 99 others); Tue, 15 Nov 2022 13:29:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39182 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231759AbiKOS3N (ORCPT ); Tue, 15 Nov 2022 13:29:13 -0500 Received: from mail-pg1-x532.google.com (mail-pg1-x532.google.com [IPv6:2607:f8b0:4864:20::532]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F205F2FFD8 for ; Tue, 15 Nov 2022 10:29:04 -0800 (PST) Received: by mail-pg1-x532.google.com with SMTP id n17so6412319pgh.9 for ; Tue, 15 Nov 2022 10:29:04 -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=S0sz/vWAjQCs/1P4XYe5XebJKXlS1wTddDknpvkdJTY80L4CblJNlZNf6XoKDRhuEN i1WcHhLkYl6QPXsvQRB2Y4duhd43JJUcZ97KxFJKHD/IMDdxsWdJeqcWaPqX9KGpJAJ2 LGRSJYtwcukxasvo4UThOYumC83uz7UNOKDKpBp7FZHQ6eWskcLIxieuQtRr8f+VqBKY +PJcSOWPQ/SCFqwKOy2Rs4ol48lCC8B7Sbix2uLQ4ef4E1nY4Q3TK2pnkg/WbUCBaLZH c+hYfCYd3fcFe/jIgmxTt/uRPIGkqRpD1uzk+v35gHufuYbOx05uR1DeFUtnaeVaw7hr vC3A== X-Gm-Message-State: ANoB5pmgPNfO81TdlooPUHx2zgGl9LQEFgMVxLqu/Z2zFcOY5Z0o/hvp Y4lIj8Rxwm0iqo9zsUG7d6Q= 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 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749588148922516787?= X-GMAIL-MSGID: =?utf-8?q?1749588148922516787?= 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: 20502 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2882826wru; Tue, 15 Nov 2022 10:34:45 -0800 (PST) X-Google-Smtp-Source: AA0mqf7gb8+7GTJBKyLT0voO9nkkvqqQ9M+GUGVXx8RcHo4SPGKCCKZ0CKxS9Kg6LykRzNdTe17J X-Received: by 2002:a63:4821:0:b0:46f:5be0:feb9 with SMTP id v33-20020a634821000000b0046f5be0feb9mr16556050pga.485.1668537285046; Tue, 15 Nov 2022 10:34:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668537285; cv=none; d=google.com; s=arc-20160816; b=co1lP+jxFj6M/h+9u0HXO9oGTgRxech5iDJzjmAtSv1HXLboX4kM22Q6wAhjiRyWcM g39FV/t5zV01Lr8weFvoQDIoKkKa0KTk+YKvkU5S1uq3WdEvfC5m39mp+eb5+YktoYAV uEprFAZ3cDdq4uys24OxE/UgI4EwhjO2mdCHrNLtNguzLmPx+LAIyozPIn8EqZBSoSkz DTGJj3PXu7fTmlbO9M85tDBjC1rVNoIDz58w5E+3AlvNRM1Elo9pJAH/s5D3Y4L3wjN4 qCL0qd69kFnBVtQxN42P6dHoxY5ScYaSRLpmbcjuTC9kDBqIJm/3VL2zp4EZEL73SJGF ePzQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=XlpGuGY3sJe9DLKH41vLTGd0LKIzHOuJ8+yi4Jjp058=; b=qmknRn/j/mFMXcGR3YJX95b48syaukuBQyqDif6B3aFbVWAHBt6JnDJHtp8H+1tLd1 vg5ebfQ9Bsb5WkEzpU1nYVKQNlBnwtoxewd+7D3HfvvE48i9Ad+ALZQxIz22FvHz5HD8 DuKTzRjH5Y0DpbF4R/vgJS2cgQz+Ofb5SdBE454+nqdSSthviL6ZNT4b915hk7crxzMo nRnIIxG1mEsAB7OVqGGO7G935HhvQGYpasTnbF/xNQlRklheOeFxUd/wkqBfm7oI4IJs 6KOkGvUUMPYbimFrdxxUkZ9WNLDPuhi1FyH4wCxp1lZmvH+S99G+nout0MtqJYy1eKPF Rp1A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=BBr8ahhu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v191-20020a6389c8000000b0046f3dfb9789si12468834pgd.477.2022.11.15.10.34.30; Tue, 15 Nov 2022 10:34:45 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=BBr8ahhu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232049AbiKOS3k (ORCPT + 99 others); Tue, 15 Nov 2022 13:29:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38880 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231855AbiKOS3O (ORCPT ); Tue, 15 Nov 2022 13:29:14 -0500 Received: from mail-pl1-x634.google.com (mail-pl1-x634.google.com [IPv6:2607:f8b0:4864:20::634]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 532022FFD6 for ; Tue, 15 Nov 2022 10:29:06 -0800 (PST) Received: by mail-pl1-x634.google.com with SMTP id v17so13959997plo.1 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=t3sz1zVnKLBG7RnvlmzrRgBeawfnLW4xAPtbKpoBaD+Y3a/fGmRTjXfL2JZNZd3CXT UwT86ivehVK5Q+A1h88WNmN9gDt2ftsVyaqK1tsq9SVWwkCAqFFRfVKYepqk/eBecR6U V8QizRPAjf3FnmHSp3f7W1n40DSlwpxl75yYkkrKeW404z59UdOUBBzUepebnCs4ULhM R2GlNwQEd+E5J/fwiDgOr4NMGLL1BCEgDt2n0itq7/p1sV1TBiyuPJg1C1aRMCgqV0dZ w5Agld/ynI6OGJ3+HzqkFblhHnCQncSj+QxHL4/5akGPsDxlc2+iVc3O47D6H+dBiujR LYag== X-Gm-Message-State: ANoB5pl0mfH90nV/XOrBlGs1THyvhTE3AbcAB9T/cPs+jSy+yM4vDPq4 NC32n41bYjAld2iGLYoPyO6e1fuizxvVjA== 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 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749588151751672292?= X-GMAIL-MSGID: =?utf-8?q?1749588151751672292?= 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: 20504 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2883352wru; Tue, 15 Nov 2022 10:36:06 -0800 (PST) X-Google-Smtp-Source: AA0mqf6etF46Fnepybj7oWaxkVFbW0KPOk4EqC+3Dis83w0yZoWyLPVFSnefPVd8tZ4eVN0XWhBs X-Received: by 2002:a05:6a00:1a43:b0:562:5f71:d188 with SMTP id h3-20020a056a001a4300b005625f71d188mr19601232pfv.57.1668537365730; Tue, 15 Nov 2022 10:36:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668537365; cv=none; d=google.com; s=arc-20160816; b=xHlQ9c9BVKLRtX/OO4uZJzdVZLC4eU8rTbILIcq7AEClO46WbbBXCoS0HKXBdZYTrm 7s/ULp9uj0Wy6yfO594o9IMH9Z5jr93bsKbzjf1OHya7RTJ5qzV/Y6OhjkKM42GdOkN2 LwkHB1rilXtMCebx+enTmrBglUPUMXjF6bE82wBPUieKidyHuENjl+MiohnDLOXDy2W4 s9bQV7XDWnLeEmLyT30Vzc7Dkf/xysy1B2NRn6aZMsATPcXK+zr06xz9xVKRzx7+5bjb 6/NHyFnF+cg5u5+D3VIUyWgI2VTuDmp9RMuvWWwI3Qa5ygSkWkSdPXDvEKgU4phNXLPl qtIQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=R+CgD6b3VuRjXvN+H9oTC8+sOdeozxCPLL1LFRJAE7c=; b=JBpBg3o21YeeDhJUH+Utkx6o+mRB1E7InpgYZWbvlhQ0kFbwBw2P6bWp1K8or6Tmr0 gYIJq/yipaZtQPV086c9zNMnqxxcLSzE4hFDa2YP/GIdmknSZ79MjkQsMbHRKgzpZevW ftvdY8ZCmWX4/f56TzICxzHefLcF9UGGI8Tb+4vCPre84deV1RY8bwUpWuLzbjeYWE6l 2yf24xkiqPBBe8YRI9n/WR/P0vd1lABol4hKpXpLGMvbIStW3w2/NgNa+5un/IRjJNS3 uyxwLuw/eQKd9Beb4rKYs3wh+jN6w2+v9ob7Ed0JPz591PHd6+AKQXOvkg7khCLGR6Nt JQtQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=DcMxx7DR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k14-20020a170902c40e00b0017f97fe774fsi14852811plk.445.2022.11.15.10.35.43; Tue, 15 Nov 2022 10:36:05 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=DcMxx7DR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229759AbiKOS3t (ORCPT + 99 others); Tue, 15 Nov 2022 13:29:49 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39198 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231935AbiKOS3P (ORCPT ); Tue, 15 Nov 2022 13:29:15 -0500 Received: from mail-pg1-x530.google.com (mail-pg1-x530.google.com [IPv6:2607:f8b0:4864:20::530]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C02C82FFEC for ; Tue, 15 Nov 2022 10:29:07 -0800 (PST) Received: by mail-pg1-x530.google.com with SMTP id q71so14037153pgq.8 for ; Tue, 15 Nov 2022 10:29:07 -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=AJWjIKm+cDgIX3OAxTI7dtyv2JVnjD6AsDDXCNsn3DpIcjeCnvktfPqGmxVdnMiLvV NceVTWl95hYILpy92+54T1Wb+IL+qAXlXgCT3ynd85YlqR//VDhg+GoIGPdm2NiB5WlH ojHFc4HKEuE+/wNVDYcfX3kH3XYoKDubQsshQBW2H9CQSeangdkeQ51/C4ft2p+lgwmH Y/WL5s8T8R3yzFIR1Q2wVOh9ziZW/JRfH8HEXBK7v0FbACtXs/kTfPXkl1N1Q+QEEdeg fJHH70YrSDO6Qb802/HO+dKUJ0OvfHia6OUuCMsrKsducDGAEsn2swwxwlgIoaYdTT69 ZSbg== X-Gm-Message-State: ANoB5pnCz+rxLrzuhZE5Ls29GT+cew3SctAxhz2ePz22wQHM9MTtrgZf 7umJQXvDBE5BLobaPDQUb7k= 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 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749588237025130120?= X-GMAIL-MSGID: =?utf-8?q?1749588237025130120?= 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: 20503 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2883048wru; Tue, 15 Nov 2022 10:35:17 -0800 (PST) X-Google-Smtp-Source: AA0mqf6v/NhYStB2vOStwsSzsB0iD5JNbsyEOD3fqJYG3TJD9YzG4K30Ig3ADnTolCnbL2vFqo2h X-Received: by 2002:a50:d65a:0:b0:461:7378:3501 with SMTP id c26-20020a50d65a000000b0046173783501mr15696813edj.35.1668537316851; Tue, 15 Nov 2022 10:35:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668537316; cv=none; d=google.com; s=arc-20160816; b=NVyy+6h1/+I3Ho01pJ0DUYSzM+r67zAxhTxBUM0Euww4tG+spvWZScqM8sRPJz90uj MdqJLPeBla8h1TPTVcvZmfUwTOkLBONK9EIE0IKN08yciLFRCL0YqYCnaVFfAnA0mt8q 7h2CqE4PV4UuTkWMdRLWJdonAP2d+/UoPX60EudCqwgPkQJBGO+qGFlIxXWtjRrx54eV LS0Dg4ENECNcBz6xoCrz2hvJqrXbLKQyeEjliKwbInjWl3n5sZBSBW8IwUWUSYsdQvkj SY4odTjfeNYzDSZPRMr3IqfUdTkaJl5rPK1MY63KulKwWz4f/sUM2bWtUg5h2vGo/PBJ jiVg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=JBJtHo7HE+ehk9NsJj/gfUpQS38HB83uuNN8e7+fjB0=; b=pdKvHI1gQdOjBVtPoUj/ix6YtNdwSJYHqyMv7Z419EAs1jluzWIvxWPyHPdKxeK6Zj QNst3RsjrrTT4fdo9kBKTIude/weBy9/sJlI19953dfl4ZVeBN+nAC2gKT3eIflHlZkA gq0KaUft0/0SNzof/7TZRmI52B8+aewmkNnhf/id3ShM2RFIFTDdKgUQkoh5lH1W7AFI lUyecy6b9eEUEdiFddLW6ANV5dH76G7fFvtMQWqMLjnYU2wj+bndqLZosH5+ODxRStxu mQ0h4bcURo3HGyQ2dASCIuCA6HV9ORBcHuXzOzz5vdvTSRmiLrYIq69GuU4obVbAm7OY GeHw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=kAbfOcrn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ds7-20020a170907724700b007adfc1509afsi12323754ejc.34.2022.11.15.10.34.46; Tue, 15 Nov 2022 10:35:16 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=kAbfOcrn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231327AbiKOS3p (ORCPT + 99 others); Tue, 15 Nov 2022 13:29:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39114 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231591AbiKOS3Q (ORCPT ); Tue, 15 Nov 2022 13:29:16 -0500 Received: from mail-pl1-x631.google.com (mail-pl1-x631.google.com [IPv6:2607:f8b0:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ED3B7303CD for ; Tue, 15 Nov 2022 10:29:08 -0800 (PST) Received: by mail-pl1-x631.google.com with SMTP id d20so13921917plr.10 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=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=eKrZj3oMY/qO1RG61LLhrfMKCYCiCW36bQELQe5oB4OqzdMd6ZjXXdF6XC40x2J6HM Dk6vAtLO1aQQGLJx5gY86WBP+wQtsfueo+Pjph+dQwJGSiSdwifkAzhB5OHhQmX2oj5E f5VwfBEd3R7cPvQtnJiXL8iP0K9vlPpEsX44L6Yt2IZZ8/cwdEdNUezjFhbY02WkAJrL MVisJqFAACFBwwpQEBXtTANwh+Di/ZpffTw3c+wJyCMsimDEdoJY0EI1gcleWvQIwj5y zbaW/BjVaX+67P3m6ouQrwRZGI4pGoWIUVHkkOKa31p/+NYqjXIn08HgGnzGR0BMgWk2 fLrg== X-Gm-Message-State: ANoB5plr5z+1dEuOEjx2OEBmi0mQnk3LIm4MG1RKWrJ3sEsr1SIBDFce jX2ACei/GK4B9yt2OoWKud4VMvkY/0hG5A== 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 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749588185265305720?= X-GMAIL-MSGID: =?utf-8?q?1749588185265305720?= 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; +}