From patchwork Thu Dec 8 20:28:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nhat Pham X-Patchwork-Id: 13068845 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 BCCABC001B2 for ; Thu, 8 Dec 2022 20:28:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1DDD38E0006; Thu, 8 Dec 2022 15:28:14 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 13F928E0001; Thu, 8 Dec 2022 15:28:14 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EFAE68E0006; Thu, 8 Dec 2022 15:28:13 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id E05D68E0001 for ; Thu, 8 Dec 2022 15:28:13 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id B40BDC0BE8 for ; Thu, 8 Dec 2022 20:28:13 +0000 (UTC) X-FDA: 80220276066.14.28C60A1 Received: from mail-pg1-f172.google.com (mail-pg1-f172.google.com [209.85.215.172]) by imf07.hostedemail.com (Postfix) with ESMTP id 032AB4001E for ; Thu, 8 Dec 2022 20:28:11 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=PtrML52y; spf=pass (imf07.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.215.172 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=1670531292; 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=Lb+KwmJkzXx8OFTtW+nTccBj0LGz6VbdaAXwuN9Uf31fkDfpRrzhaYNhvWi7DbLoGkSw+w KqQKoX0SjsjN4fLUdBugXH6tyuJoJKm4F9t8OTMjSS+GgDJI8vsKQ5hKKlSf9167YUIOB7 kNnpEHr21INmTNcirwJWnHHkhIDZ9B0= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=PtrML52y; spf=pass (imf07.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.215.172 as permitted sender) smtp.mailfrom=nphamcs@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1670531292; a=rsa-sha256; cv=none; b=JIhCD/18AmoPOszJywQEr0JSBd2gKGxcfiVjUCNTOaoINS08ob54vvyPBvcPr4X7c6uKpF MoIGJU/wkrOoYzBoArgVD2cNAF/lmNaA18QDXJ50ojdot/Yd96IQZCNmNSyV5mZFeTodG9 /kmPr4bTm5KB2Mub0pbMZ/pdir/9Z/Y= Received: by mail-pg1-f172.google.com with SMTP id q71so2087046pgq.8 for ; Thu, 08 Dec 2022 12:28:11 -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=PtrML52y4++BjkDz048pa6CUOhJB/mkeWNTiPY7aI2UbgF7O0oTXxRSB+Rgt+XOkh2 IhRH1bB7exc+5heFlRaaYr1yjjAwmfAuerp1aWipjPeaCOdr512Ka1FELYXazYxmhQEi SI7xS7B+WXTt+l2S403kfG2hbvrX5IAdut2oWDHfpaLlRGt4YV6g6R2N/Y+M0OIVauM4 yspqne9tauDtNmi+n/6QI2qbmUkn/MYPIpdZcxb09OW88KyKsov7q5fHtFuGQCLf0/61 TA66+MiESyfsohxgdTOk04PCX9QDVLvzcic6TUKUmrBvMvg3B2oikbP6Edti5vGkX47C FKMQ== 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=rBhftLAHwFPofQQZltmF9p52QRgEhPAwVUCWLT7+aGCR4Cot77d/vDZLutXl3fngP0 T4kxHtgIqJXmeWTX9IQSUI0aL8PmfJuKwF3+fbqQU47zsU8bkvkn+mRdrubNGOeydDTY QbGLwb77UJlVgxJIBzGk3hG1Rr5FzX8kH3d8h3pIf8XoQs8JKCP1n9h/iD60032YYEip 7sKr5vqfjZmWkZE97JPbpmpUKeT6IF6d/D1SmUGB9G4zHC/IajjlM2031KYcARJPSsoa hU6emGDy+fMuqWsouBlNq4gIUgUqynO6OdmtHntAwOoCERW3LWjgVFVYBdXuOGNnH+D3 Czhg== X-Gm-Message-State: ANoB5pm6Dbo9Z/ZTGXOYDeD8G1mwEZrQ0RextD+mH8/s+oit7Os1iytQ 7flxa2Ka3Tpqye4W4kYi+HA= X-Google-Smtp-Source: AA0mqf4PzirRAa+DHb5HDZfpPwkKugYCrcQU4IMR06a44BnH7VGJOBKJqvkTVtEYWESsKuUDvBw8gQ== X-Received: by 2002:a62:3142:0:b0:56d:8d19:f331 with SMTP id x63-20020a623142000000b0056d8d19f331mr99706326pfx.7.1670531290779; Thu, 08 Dec 2022 12:28:10 -0800 (PST) Received: from localhost (fwdproxy-prn-008.fbsv.net. [2a03:2880:ff:8::face:b00c]) by smtp.gmail.com with ESMTPSA id b29-20020aa7951d000000b0057555d35f79sm15702101pfp.101.2022.12.08.12.28.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Dec 2022 12:28:10 -0800 (PST) From: Nhat Pham To: akpm@linux-foundation.org Cc: hannes@cmpxchg.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, bfoster@redhat.com, willy@infradead.org, kernel-team@meta.com Subject: [PATCH v3 1/4] workingset: fix confusion around eviction vs refault container Date: Thu, 8 Dec 2022 12:28:05 -0800 Message-Id: <20221208202808.908690-2-nphamcs@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20221208202808.908690-1-nphamcs@gmail.com> References: <20221208202808.908690-1-nphamcs@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 032AB4001E X-Stat-Signature: q3cfdn7tghk9d7if1wgc7mfoi9wc3ytp X-Rspam-User: X-HE-Tag: 1670531291-734828 X-HE-Meta: U2FsdGVkX1+xRK/zafcI9b1CGB19W+vQYe6RCTFSNsbZHBgYmkWan7U22VARLf0kqPbjrPgE6PHJ6NKshf+EUyKn9rLAM841cJtd4IA4r5fRtCUIfV95gNVnX1InLsN6pWmYtpT5uTDIsnuaGaHB0+4j48h9o109DTtdB2vveb7jUs3GTOobPlcZ0W5xdgvuR40o6/lWXXXsJiW4fk+EwzcaXOj6t4eHnJt7kZsLZvgkJ1RoKCepz7mGxgj9rh39iuEvGFN984iIJfeGy1mqUOn2Z8UClTLq+Pe1bfqbTLVYpCQ2TmiS0xyXw+lBd7NsWaDBIlIeF6OaUKKWap3AzgpWI//g94bPmxd8uUw17HynCddUlOpfbBN8T7el7zN2fwB6ACnnzx6uNL61s2ylgrQri3D/Kla00mhaAS3F4qQeIQEKxhQKQRZ+ChcExf5P8rcTpj2UZaqzKUeAlHJm76seAqyiOmRqZCDpVxRxkUx1MZ36hEgrycrq5sKAM5ise4jpSBf+eUsApKKnZs7EJ1aQ+uhin9YF1bDPQgr4+FBv5dPBIkrcYivl9ja1lkeAf7EaGUm/1QN/qeZ1No+lZ6dvHWZQSamyxacLzJXWy4M/B8EuusSnfOLxJ5H2Zw6Cl3ZSQy+mNJucKrE5EsDXsUJFXrCCrKajYw8oCjvQXLBdKfetrkfC46ZIBUiAye6nsknDFJ54U8D+exDS3ZKdmWc9ls/p93pVb/fjSCazRFE+woNxYTNRrzDoycBzg7/xMUURcJEjP1ypUHiW0vSyWr8KC8ciCdZ4jOV0LWagbWKTtDl5ZhnVU64mgHPx9vVKj/Zlumzdrk+WhgHShk/qWUz8oP0QiTEUWcENuCiVwFV0yhwxOAQIqLIWN5qBMWhFtdnwARJblmnRrI7y3bkj7Zk7Q0sk9w3SQzpr3XGHPLf0BQhob9WbXzxdUusbFFtmz7m8DgjUu3M142rvRSc l6IrP9ac jU8zBap6ywCPYa+t4u0IlAq/FurMjWbBaVztKNa8PLQstKwCwLfWxRnWvuf4jnyqLwDv/rkIN0mU141ygyHffhLeNEHOkFDv8nD15JhHMEYQ/z5NoFLo01SWOl6qayIen6zGz X-Bogosity: Ham, tests=bogofilter, spamicity=0.000063, 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(-) -- 2.30.2 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 Thu Dec 8 20:28:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nhat Pham X-Patchwork-Id: 13068846 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 C5712C4167B for ; Thu, 8 Dec 2022 20:28:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5D5F88E0007; Thu, 8 Dec 2022 15:28:15 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 584F18E0001; Thu, 8 Dec 2022 15:28:15 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3FE2F8E0007; Thu, 8 Dec 2022 15:28:15 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 2FA0D8E0001 for ; Thu, 8 Dec 2022 15:28:15 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id EE73F40375 for ; Thu, 8 Dec 2022 20:28:14 +0000 (UTC) X-FDA: 80220276108.04.CE26FDE Received: from mail-pj1-f43.google.com (mail-pj1-f43.google.com [209.85.216.43]) by imf05.hostedemail.com (Postfix) with ESMTP id 48AED100016 for ; Thu, 8 Dec 2022 20:28:13 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=bEy+D57v; spf=pass (imf05.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.216.43 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=1670531293; 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=HrPDf/9rdHRb66FM/5ioNOV5BcRP2aeYNAosdnP8fIw=; b=7gbhKx/ATuyIR0XcLXk80zskYE7dQYr3VarwfEWKQtNncJTAT8HsToo9CTJ0bk5uITsvd7 ob8SLYDJ89MPdCrY9E9h88QdOvJNaL1wXGdeoG0TxSshe7z+icRIJlGKmKRe587VNGjuEf sY1vEr9d37xJACIsz4l/jrpFBjN1H3U= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=bEy+D57v; spf=pass (imf05.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.216.43 as permitted sender) smtp.mailfrom=nphamcs@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1670531293; a=rsa-sha256; cv=none; b=b6/CKGOMEBJlXxcjnLuQEsQ9fIxPgDLbaNdHFuvPSGU/m/Mup9e4ad+yNbYvjsT4yDpl1w GWVwChyNmRmJJgqTfslbh6J6UcPSOI70Fg0PGAgCoE3RzPeKleVR2WPa5LicohUKegWvu0 xpcsjZV6JPpQ+DR/LgOMA2s1sjS2iL4= Received: by mail-pj1-f43.google.com with SMTP id w4-20020a17090ac98400b002186f5d7a4cso5881770pjt.0 for ; Thu, 08 Dec 2022 12:28:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=HrPDf/9rdHRb66FM/5ioNOV5BcRP2aeYNAosdnP8fIw=; b=bEy+D57vWPlDdJZNJwhqWKPdncwerv9AN9R7aXydKslKdjGvubZG5uBvBSqdZ+vgy6 SeOSaIAKQZOgVVntFUNb2wi6PTmDfVoP7YlBZdhKJqoaW+fkwb9jEo2UeoJ6n1RKubqK /acb7+DhSOCvImRfxxur15emgCdH37yK2sBoJ4nINcbOxDXYCYWlAt0TT94g8zQOi1sf kidz6a460J9c7P2EFdtkmhMEiiprO0YyYe9iIeLy0I/SCj91cumqevDn5C4ijKhEovFq XtrDWJ6C2D1ZGzwI8FXRT1WyJfQ8E+6/EktACCCp7JzR6XWJ+E2soDQ4Kd9n4Tnyr7n/ SGoQ== 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=HrPDf/9rdHRb66FM/5ioNOV5BcRP2aeYNAosdnP8fIw=; b=URl3vxYLF7s7YSCSYwLcIPMjbxNYG69NjQ0qNMKlvM9hnbjHFSzog4Q8rlfAKpJbN5 y4f5ZSfrIjO731Uugvci8VNrlCAK0p0eftSWPNnTIAhG2E1BHyYHcmCjyoTZV7jhFcYe PxzrhPGNeMvbDRUDGK8iNqmrdYy5H4ouC1vvsxEq6VSKqv6qJLDlfLOXTnxaFnPGm3zS UwtrokAEI0CXz/gka1LHxlhmpMTP15eNY8A7htICakYl9dpY7ORdGP1JJcamyqfCVwJw 9PzlK4yYrb3XvhgH6JRlAop4B0ZYlKOYDZ2KV0Zpp/yimtOJxyv69k4kcsQM4UQrwZz9 7yGg== X-Gm-Message-State: ANoB5plKdkqK7LnfYHX4zM6M0fRjjEfFtSQXhqb9YRreBObVAWDf1Qyg r3D/NdLcVV6H80Oes2f+uqo= X-Google-Smtp-Source: AA0mqf5mZzIIFsAFsksyghH585zqEwzLSeOa+lkEJP2NOFdmLWBycKzJiVfedchfsSL0YoI9CMmwIg== X-Received: by 2002:a17:90a:e50e:b0:219:41d0:8fc1 with SMTP id t14-20020a17090ae50e00b0021941d08fc1mr50644688pjy.173.1670531292091; Thu, 08 Dec 2022 12:28:12 -0800 (PST) Received: from localhost (fwdproxy-prn-014.fbsv.net. [2a03:2880:ff:e::face:b00c]) by smtp.gmail.com with ESMTPSA id x14-20020a170902ec8e00b001897916be2bsm17035238plg.268.2022.12.08.12.28.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Dec 2022 12:28:11 -0800 (PST) From: Nhat Pham To: akpm@linux-foundation.org Cc: hannes@cmpxchg.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, bfoster@redhat.com, willy@infradead.org, kernel-team@meta.com Subject: [PATCH v3 2/4] workingset: refactor LRU refault to expose refault recency check Date: Thu, 8 Dec 2022 12:28:06 -0800 Message-Id: <20221208202808.908690-3-nphamcs@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20221208202808.908690-1-nphamcs@gmail.com> References: <20221208202808.908690-1-nphamcs@gmail.com> MIME-Version: 1.0 X-Stat-Signature: o5k3zc6du6destmntjw1mxj5psnkdaxi X-Rspam-User: X-Rspamd-Queue-Id: 48AED100016 X-Rspamd-Server: rspam06 X-HE-Tag: 1670531293-27231 X-HE-Meta: U2FsdGVkX1+4wpkM1qtG6S3XkJtzGz8kM7Uz0uN0KroKrM+mkV7RgO8iwNFnnPcc+RfVsLLoMtEcaF7f9hc3aptrGq3XNmbxuykeouEW4lcZtYlx/uWwHuhJukVmIjYXYUaqw9oMoGIaDAbXhAauZa0S9puUEUQzgylhPGUVung6F8M6zX74lRCE6HcespLTNwulfPx1ec9B/tv1Pr27SWmymTY9uzWRtqzIabuIgc5cu67Hu1wH43s6CjOMnrGybFlLrea3j0D1HYo6sgE/vNQYGshzeHmVinPlKjTzU+35Ecphk3Le8/wP//5yT11zoJlYBX1mUNSJ5/c8+ifGkn2hBBXVYqGIKgQsJZlNb8TBCBa6qg0iz3S0PMxmjHlVxsv6MSSCXvst2z5FrF5J3jJ92kHwRA+FFyX/StDcEv/YS6RUNL99tP1+oqRQRB26VTnd8YV5VMlvYp2sM464LpmywhoYKFWvjEh2zIFYadsQ0qcL3r/hf36CChOYisA7vg5orkcafj6LTUXtRe/JaI4VIQ56lh/gaMZ6qtuC1ANrpZDcxGQd6FVYbbo7MNb2ZDdMbdOaabw58XQoZnfvi0CLMmnideKXkr7ODpHtw0lkoou/61NAJ6H6eyOqNuAus/pSo98IhPrnBFz9Ba2Uuc41cOb6+om4vaJ9f4urtIJoiLPpvsgwqluGcR+61bQciTqD80PSlZEHNksWlp81LlXJ3v2moKlk088gQBBzDmVVP8Zjd+SUT7/LusUpZ98qjanlh2djg91fcI9J5gfmPh0zdbmCcI5huck7tN8mLUjHMsRUl7VISSStp1fWhcvRXbdQjY6W8Tw0L9/igI7S4lczghEiH82j3EVdayDSZgKDWxTMcce7OqYWfdIuyl8xhGRmEriHGw1g25GWb3T08qBaOPz3M/le/4PX21GXkce7q/G0wMFcvGJrNN9dGWf2H/wvWhsE4s6xjAWiTVC cpzaiVh6 K9XUd3EO1rOOzo65bekD5QeqQOVE52ZZ3TP7J1km4LkCGbv7Hj1gTwnljw3KNqQOftpQwxgieYfJdLzehzCj0Y/UIWFkOtA4r8WqjioQhU/pVFmas+ucbkCoNaReN5veTyI3n X-Bogosity: Ham, tests=bogofilter, spamicity=0.000003, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: In preparation for computing recently evicted pages in cachestat, refactor workingset_refault and lru_gen_refault to expose a helper function that would test if an evicted page is recently evicted. Signed-off-by: Nhat Pham --- include/linux/swap.h | 1 + mm/workingset.c | 129 ++++++++++++++++++++++++++++++------------- 2 files changed, 92 insertions(+), 38 deletions(-) -- 2.30.2 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..4a06869392a1 100644 --- a/mm/workingset.c +++ b/mm/workingset.c @@ -244,6 +244,33 @@ static void *lru_gen_eviction(struct folio *folio) return pack_shadow(mem_cgroup_id(memcg), pgdat, token, refs); } +/* + * Test if the folio is recently evicted. + * + * As a side effect, also populates the references with + * values unpacked from the shadow of the evicted folio. + */ +static bool lru_gen_test_recent(void *shadow, bool file, bool *workingset) +{ + struct mem_cgroup *eviction_memcg; + struct lruvec *lruvec; + struct lru_gen_struct *lrugen; + unsigned long min_seq; + + int memcgid; + struct pglist_data *pgdat; + unsigned long token; + + unpack_shadow(shadow, &memcgid, &pgdat, &token, workingset); + eviction_memcg = mem_cgroup_from_id(*memcgid); + + lruvec = mem_cgroup_lruvec(eviction_memcg, *pgdat); + lrugen = &lruvec->lrugen; + + min_seq = READ_ONCE(lrugen->min_seq[file]); + return !((*token >> LRU_REFS_WIDTH) != (min_seq & (EVICTION_MASK >> LRU_REFS_WIDTH))); +} + static void lru_gen_refault(struct folio *folio, void *shadow) { int hist, tier, refs; @@ -306,6 +333,11 @@ static void *lru_gen_eviction(struct folio *folio) return NULL; } +static bool lru_gen_test_recent(void *shadow, bool file, bool *workingset) +{ + return true; +} + static void lru_gen_refault(struct folio *folio, void *shadow) { } @@ -373,40 +405,31 @@ void *workingset_eviction(struct folio *folio, struct mem_cgroup *target_memcg) folio_test_workingset(folio)); } -/** - * workingset_refault - Evaluate the refault of a previously evicted folio. - * @folio: The freshly allocated replacement folio. - * @shadow: Shadow entry of the evicted folio. +/* + * Test if the folio is recently evicted by checking if + * refault distance of shadow exceeds workingset size. * - * Calculates and evaluates the refault distance of the previously - * evicted folio in the context of the node and the memcg whose memory - * pressure caused the eviction. + * As a side effect, populate workingset with the value + * unpacked from shadow. */ -void workingset_refault(struct folio *folio, void *shadow) +bool workingset_test_recent(void *shadow, bool file, bool *workingset) { - bool file = folio_is_file_lru(folio); struct mem_cgroup *eviction_memcg; struct lruvec *eviction_lruvec; unsigned long refault_distance; unsigned long workingset_size; - struct pglist_data *pgdat; - struct mem_cgroup *memcg; - unsigned long eviction; - struct lruvec *lruvec; unsigned long refault; - bool workingset; + int memcgid; - long nr; + struct pglist_data *pgdat; + unsigned long eviction; - if (lru_gen_enabled()) { - lru_gen_refault(folio, shadow); - return; - } + if (lru_gen_enabled()) + return lru_gen_test_recent(shadow, file, workingset); - unpack_shadow(shadow, &memcgid, &pgdat, &eviction, &workingset); + unpack_shadow(shadow, &memcgid, &pgdat, &eviction, workingset); eviction <<= bucket_order; - rcu_read_lock(); /* * Look up the memcg associated with the stored ID. It might * have been deleted since the folio's eviction. @@ -425,7 +448,8 @@ void workingset_refault(struct folio *folio, void *shadow) */ eviction_memcg = mem_cgroup_from_id(memcgid); if (!mem_cgroup_disabled() && !eviction_memcg) - goto out; + return false; + eviction_lruvec = mem_cgroup_lruvec(eviction_memcg, pgdat); refault = atomic_long_read(&eviction_lruvec->nonresident_age); @@ -447,21 +471,6 @@ void workingset_refault(struct folio *folio, void *shadow) */ refault_distance = (refault - eviction) & EVICTION_MASK; - /* - * The activation decision for this folio is made at the level - * where the eviction occurred, as that is where the LRU order - * during folio reclaim is being determined. - * - * However, the cgroup that will own the folio is the one that - * is actually experiencing the refault event. - */ - nr = folio_nr_pages(folio); - memcg = folio_memcg(folio); - pgdat = folio_pgdat(folio); - lruvec = mem_cgroup_lruvec(memcg, pgdat); - - mod_lruvec_state(lruvec, WORKINGSET_REFAULT_BASE + file, nr); - mem_cgroup_flush_stats_delayed(); /* * Compare the distance to the existing workingset size. We @@ -483,8 +492,51 @@ void workingset_refault(struct folio *folio, void *shadow) NR_INACTIVE_ANON); } } - if (refault_distance > workingset_size) + + return refault_distance <= workingset_size; +} + +/** + * workingset_refault - Evaluate the refault of a previously evicted folio. + * @folio: The freshly allocated replacement folio. + * @shadow: Shadow entry of the evicted folio. + * + * Calculates and evaluates the refault distance of the previously + * evicted folio in the context of the node and the memcg whose memory + * pressure caused the eviction. + */ +void workingset_refault(struct folio *folio, void *shadow) +{ + bool file = folio_is_file_lru(folio); + struct pglist_data *pgdat; + struct mem_cgroup *memcg; + struct lruvec *lruvec; + bool workingset; + long nr; + + if (lru_gen_enabled()) { + lru_gen_refault(folio, shadow); + return; + } + + rcu_read_lock(); + + nr = folio_nr_pages(folio); + memcg = folio_memcg(folio); + pgdat = folio_pgdat(folio); + lruvec = mem_cgroup_lruvec(memcg, pgdat); + + if (!workingset_test_recent(shadow, file, &workingset)) { + /* + * The activation decision for this folio is made at the level + * where the eviction occurred, as that is where the LRU order + * during folio reclaim is being determined. + * + * However, the cgroup that will own the folio is the one that + * is actually experiencing the refault event. + */ goto out; + } folio_set_active(folio); workingset_age_nonresident(lruvec, nr); @@ -498,6 +550,7 @@ void workingset_refault(struct folio *folio, void *shadow) mod_lruvec_state(lruvec, WORKINGSET_RESTORE_BASE + file, nr); } out: + mod_lruvec_state(lruvec, WORKINGSET_REFAULT_BASE + file, nr); rcu_read_unlock(); } From patchwork Thu Dec 8 20:28:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nhat Pham X-Patchwork-Id: 13068848 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 26F5FC4167B for ; Thu, 8 Dec 2022 20:28:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C4DC88E0001; Thu, 8 Dec 2022 15:28:17 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BCDBA8E000A; Thu, 8 Dec 2022 15:28:17 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9F1CB8E0001; Thu, 8 Dec 2022 15:28:17 -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 7C4C38E0008 for ; Thu, 8 Dec 2022 15:28:17 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 52D0F14081C for ; Thu, 8 Dec 2022 20:28:17 +0000 (UTC) X-FDA: 80220276234.24.444EAA9 Received: from mail-pf1-f170.google.com (mail-pf1-f170.google.com [209.85.210.170]) by imf06.hostedemail.com (Postfix) with ESMTP id 95C8D180012 for ; Thu, 8 Dec 2022 20:28:14 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=Sj1r05Ux; spf=pass (imf06.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.210.170 as permitted sender) smtp.mailfrom=nphamcs@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1670531294; a=rsa-sha256; cv=none; b=ptGkFaFIou90C3ScnVBapvTh8qiq6Wc7Rjy7LqUCZedAC2fOnZ5i20mddnxmn4dLcbKmkk ecTkZr4z5yZdnAQ7hrs2jG0cdgJfoB2NXp4Ef5BwimfjodBdGaFJvkn69pSeUHbzlXUpsG VDbXFs1h9Iu4Mq21iIMZYUIf8kpWz+c= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=Sj1r05Ux; spf=pass (imf06.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.210.170 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=1670531294; 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=6OziiiRLNSzXipRtwNVO5s9HQHhNZWgMZRqqlEYKjCM=; b=bL1Zai7Ij3e8tl+ZcRYzXRz8BZTnzfzxZ07t9GYamgT672CG8bfk1MDPMYaNlkEOKJ7HMY oHgSRKofIFexctz8NkE3EWI7aSRfhWNmjCVqUJKLfT1uTk37aShJQoUQhE6Uw7L+cFdHaz Z4s5HDDNt96h8+6ZW94s+CFOWTLy3XA= Received: by mail-pf1-f170.google.com with SMTP id a14so2199760pfa.1 for ; Thu, 08 Dec 2022 12:28:14 -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=6OziiiRLNSzXipRtwNVO5s9HQHhNZWgMZRqqlEYKjCM=; b=Sj1r05Uxh4eVh/6W58ylqxHpfLInFjmnJeqicXpequk2IVw/m7OUcDMB4x4cS43b1y qHAoTWDfw6IggNsUy+yQjBC9fqr/Te+hZ08h7UIb7sD3LZvROoXD6eL5gRVyfCX41VQX UhZmPwipNnCMFaee1EXQnZTxoKst8Vu/9Ow0NdvRbRZdg8Ven5hXjI5LXOJF1fTCZlWp HNd2tDMp7CQz/fOeAiRUGse/Qs844DdR5yFJuZ0B1u2nyRuCunKWWinN0rUhPNj/Bnh6 5oRYVaObj9XMCT0+cZ/QSh9/3sMpW+o8F5fRTz3TDt7Hq6oWEjY7LEp7iwdFz0NT5LDY IrRg== 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=6OziiiRLNSzXipRtwNVO5s9HQHhNZWgMZRqqlEYKjCM=; b=Asc+U95RofKO7Y493s4berCYu6ymW9f956lSeNM/denvICA9E1L9ESTm0V/yUSzTYk 9QcDRDoji3V6i831SMi6N91oxkdowaxBSQ6l6SW8r5+6J3bK9ubXVxDDnwl1y3b/v53W xWAoU6gatxqUjJXupfvN8CAGZNTBp8lalTB/gnbp/HReLraqrkzZGuHCxsVg77roNznW 1FBSXdhmEtomWREeIOXIZGhe/KvwDocxvCXb0SEtWyAUruebbHNlIK0hcRSYd7c2sJ3a u+n85DPglo8C6E+B0a3Y4uRjOGzHwN49P+RNMiYP231+fOgtwYM7HTcdyMrEmO5Wa6Xw qztg== X-Gm-Message-State: ANoB5pk1Uu1rmzckCssNL7jFV/nbDN2+fzw/cmykVt7ar3lQw6gezMJS GiZnrNejXgGACgXB+ixyx3w= X-Google-Smtp-Source: AA0mqf6ywRIfSzRQ1duTnwiS9H9Br9ok+5JvQ3sujZRbMhXsr5F8exqSPgt9Efffxc1wUpIGL6Dgxg== X-Received: by 2002:a63:1f20:0:b0:478:3376:b7bf with SMTP id f32-20020a631f20000000b004783376b7bfmr42072708pgf.5.1670531293423; Thu, 08 Dec 2022 12:28:13 -0800 (PST) Received: from localhost (fwdproxy-prn-018.fbsv.net. [2a03:2880:ff:12::face:b00c]) by smtp.gmail.com with ESMTPSA id t15-20020a63f34f000000b0046fefb18a09sm13280771pgj.91.2022.12.08.12.28.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Dec 2022 12:28:12 -0800 (PST) From: Nhat Pham To: akpm@linux-foundation.org Cc: hannes@cmpxchg.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, bfoster@redhat.com, willy@infradead.org, kernel-team@meta.com Subject: [PATCH v3 3/4] cachestat: implement cachestat syscall Date: Thu, 8 Dec 2022 12:28:07 -0800 Message-Id: <20221208202808.908690-4-nphamcs@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20221208202808.908690-1-nphamcs@gmail.com> References: <20221208202808.908690-1-nphamcs@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 95C8D180012 X-Rspamd-Server: rspam01 X-Stat-Signature: 3f95wtmtirwb1uxcs18wq76jjic815hz X-HE-Tag: 1670531294-17255 X-HE-Meta: U2FsdGVkX18ljn7VSX/Z/Vvk/yD5DQG7nKKXhEScx5V1uS9FYlVQutRZgfQD4e+OJXN8A35ZAnPdJoqw9L9LnU/C6KMDi2SZCDWqQxn0rsduvAFVX0aqLoXa1ICHiPE9QC6Vnej2pbCsXd4HmHJDcpQeSy9TZtmV7O3Xws0SW5HTZRH4Ql/gBW6hcWeI0+jP1vW6DJlbU0+mzhgIQo2DndnpgGqiObAq8dpmZHOETVKyyapieGEywkEVpLzq17WY3mxu+XvsXvQ0WtGptabmbk+JWldgCp6Ej+oOwIxyDpoOduW409kZ/ZAawAk6B5xA502q2Q3WZ+WaI4w6+pl71c537wIckxmlsN4c5XyCdq+rIpmZeyCsi6p4Dr3aJ99whzYPRIjFWJ5PkrJbvoXB/RVr+XDK3L6/7w8C885y8eFT/jl8Z48D9dQUOWp/dpiO7e6iS4/QqUL25SFUfZ1H2ZXyYSLwQ6ccQDJa2K4/cDWvUMrXYX7YmX4Ys0cRPrmLPiSyGnksIBS4h6vOhf6cz4PePtSYQXy4jRXJadKNOVGUn7Sl10RQiZG0ISjgoe5aRVNt5GvsmlSYaJ9AVy5OxLu/z3xmSaOHviz88usmZT/Qi1KNcANhwlqQ7QNBsci1KouBWtZoy0y8047h2te7AruVmH5trCBgoR3pXTgjRzMadCGconvj5O2M4pX9Mc/0Z0ueE0qZFCi0MywMheN5EZ8nQWdOWJyayZ2/VkCdDTptrWRuINBhoUNzjuUYINMw0GUy+xerb9HvyyT6ciJdX5/v0TpvdORV/QkMY826xTK5RhuG+g2fI8WqxVl8ArBuCcO53riSNZIz6C813QyPJ2z+hDosJ/lJ+lsi2C5VwaKppTGRhaVXKnhi6v5953jBwFX4uYFOnF3YoXXVpxhUFYekhzLNDVKZryjHH4eOc/011sM+jzW+z0zlYx886gUTxl3HE6rI4awFiZXfeuo ZTOLgP6p BdoEIi0VUde/mMy90VOxsuecBrsoPLSQJ28DAG3WjUaIMZTSsP9npKTXFHEYT+ltnELKlbAfhsK9SlDs8gwyjTY7EpUwbZXeHw55y9AjRbR+zRUqISVwdcWS0a/Stpv4NjHED 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 { __u64 nr_cache; __u64 nr_dirty; __u64 nr_writeback; __u64 nr_evicted; __u64 nr_recently_evicted; }; int cachestat(unsigned int fd, off_t off, size_t len, size_t cstat_size, struct cachestat *cstat, unsigned int flags); DESCRIPTION cachestat() queries the number of cached pages, number of dirty pages, number of pages marked for writeback, number of (recently) evicted pages, in the bytes range given by `off` and `len`. These values are returned in a cachestat struct, whose address is given by the `cstat` argument. The `off` and `len` arguments must be non-negative integers. If `len` > 0, the queried range is [`off`, `off` + `len`]. If `len` == 0, we will query in the range from `off` to the end of the file. `cstat_size` allows users to obtain partial results. The syscall will copy the first `csstat_size` bytes to the specified userspace memory. `cstat_size` must be a non-negative value that is no larger than the current size of the cachestat struct. The `flags` argument is unused for now, but is included for future extensibility. User should pass 0 (i.e no flag specified). 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 invalid `cstat_size` or `flags` EBADF invalid file descriptor. Signed-off-by: Nhat Pham Acked-by: Geert Uytterhoeven [m68k] --- MAINTAINERS | 7 ++ arch/alpha/kernel/syscalls/syscall.tbl | 1 + arch/arm/tools/syscall.tbl | 1 + arch/ia64/kernel/syscalls/syscall.tbl | 1 + arch/m68k/kernel/syscalls/syscall.tbl | 1 + arch/microblaze/kernel/syscalls/syscall.tbl | 1 + arch/parisc/kernel/syscalls/syscall.tbl | 1 + arch/powerpc/kernel/syscalls/syscall.tbl | 1 + arch/s390/kernel/syscalls/syscall.tbl | 1 + arch/sh/kernel/syscalls/syscall.tbl | 1 + arch/sparc/kernel/syscalls/syscall.tbl | 1 + arch/x86/entry/syscalls/syscall_32.tbl | 1 + arch/x86/entry/syscalls/syscall_64.tbl | 1 + arch/xtensa/kernel/syscalls/syscall.tbl | 1 + include/linux/syscalls.h | 3 + include/uapi/asm-generic/unistd.h | 5 +- include/uapi/linux/mman.h | 9 ++ init/Kconfig | 10 ++ kernel/sys_ni.c | 1 + mm/Makefile | 1 + mm/cachestat.c | 117 ++++++++++++++++++++ 21 files changed, 165 insertions(+), 1 deletion(-) create mode 100644 mm/cachestat.c -- 2.30.2 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/alpha/kernel/syscalls/syscall.tbl b/arch/alpha/kernel/syscalls/syscall.tbl index 8ebacf37a8cf..1f13995d00d7 100644 --- a/arch/alpha/kernel/syscalls/syscall.tbl +++ b/arch/alpha/kernel/syscalls/syscall.tbl @@ -490,3 +490,4 @@ 558 common process_mrelease sys_process_mrelease 559 common futex_waitv sys_futex_waitv 560 common set_mempolicy_home_node sys_ni_syscall +561 common cachestat sys_cachestat diff --git a/arch/arm/tools/syscall.tbl b/arch/arm/tools/syscall.tbl index ac964612d8b0..8ebed8a13874 100644 --- a/arch/arm/tools/syscall.tbl +++ b/arch/arm/tools/syscall.tbl @@ -464,3 +464,4 @@ 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 diff --git a/arch/ia64/kernel/syscalls/syscall.tbl b/arch/ia64/kernel/syscalls/syscall.tbl index 72c929d9902b..f8c74ffeeefb 100644 --- a/arch/ia64/kernel/syscalls/syscall.tbl +++ b/arch/ia64/kernel/syscalls/syscall.tbl @@ -371,3 +371,4 @@ 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 diff --git a/arch/m68k/kernel/syscalls/syscall.tbl b/arch/m68k/kernel/syscalls/syscall.tbl index b1f3940bc298..4f504783371f 100644 --- a/arch/m68k/kernel/syscalls/syscall.tbl +++ b/arch/m68k/kernel/syscalls/syscall.tbl @@ -450,3 +450,4 @@ 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 diff --git a/arch/microblaze/kernel/syscalls/syscall.tbl b/arch/microblaze/kernel/syscalls/syscall.tbl index 820145e47350..858d22bf275c 100644 --- a/arch/microblaze/kernel/syscalls/syscall.tbl +++ b/arch/microblaze/kernel/syscalls/syscall.tbl @@ -456,3 +456,4 @@ 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 diff --git a/arch/parisc/kernel/syscalls/syscall.tbl b/arch/parisc/kernel/syscalls/syscall.tbl index 8a99c998da9b..7c84a72306d1 100644 --- a/arch/parisc/kernel/syscalls/syscall.tbl +++ b/arch/parisc/kernel/syscalls/syscall.tbl @@ -448,3 +448,4 @@ 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 diff --git a/arch/powerpc/kernel/syscalls/syscall.tbl b/arch/powerpc/kernel/syscalls/syscall.tbl index 2bca64f96164..937460f0a8ec 100644 --- a/arch/powerpc/kernel/syscalls/syscall.tbl +++ b/arch/powerpc/kernel/syscalls/syscall.tbl @@ -530,3 +530,4 @@ 448 common process_mrelease sys_process_mrelease 449 common futex_waitv sys_futex_waitv 450 nospu set_mempolicy_home_node sys_set_mempolicy_home_node +451 common cachestat sys_cachestat diff --git a/arch/s390/kernel/syscalls/syscall.tbl b/arch/s390/kernel/syscalls/syscall.tbl index 799147658dee..7df0329d46cb 100644 --- a/arch/s390/kernel/syscalls/syscall.tbl +++ b/arch/s390/kernel/syscalls/syscall.tbl @@ -453,3 +453,4 @@ 448 common process_mrelease sys_process_mrelease sys_process_mrelease 449 common futex_waitv sys_futex_waitv sys_futex_waitv 450 common set_mempolicy_home_node sys_set_mempolicy_home_node sys_set_mempolicy_home_node +451 common cachestat sys_cachestat sys_cachestat diff --git a/arch/sh/kernel/syscalls/syscall.tbl b/arch/sh/kernel/syscalls/syscall.tbl index 2de85c977f54..97377e8c5025 100644 --- a/arch/sh/kernel/syscalls/syscall.tbl +++ b/arch/sh/kernel/syscalls/syscall.tbl @@ -453,3 +453,4 @@ 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 diff --git a/arch/sparc/kernel/syscalls/syscall.tbl b/arch/sparc/kernel/syscalls/syscall.tbl index 4398cc6fb68d..faa835f3c54a 100644 --- a/arch/sparc/kernel/syscalls/syscall.tbl +++ b/arch/sparc/kernel/syscalls/syscall.tbl @@ -496,3 +496,4 @@ 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 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/arch/xtensa/kernel/syscalls/syscall.tbl b/arch/xtensa/kernel/syscalls/syscall.tbl index 52c94ab5c205..2b69c3c035b6 100644 --- a/arch/xtensa/kernel/syscalls/syscall.tbl +++ b/arch/xtensa/kernel/syscalls/syscall.tbl @@ -421,3 +421,4 @@ 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 diff --git a/include/linux/syscalls.h b/include/linux/syscalls.h index a34b0f9a9972..50f8c6999d99 100644 --- a/include/linux/syscalls.h +++ b/include/linux/syscalls.h @@ -72,6 +72,7 @@ struct open_how; struct mount_attr; struct landlock_ruleset_attr; enum landlock_rule_type; +struct cachestat; #include #include @@ -1056,6 +1057,8 @@ asmlinkage long sys_memfd_secret(unsigned int flags); asmlinkage long sys_set_mempolicy_home_node(unsigned long start, unsigned long len, unsigned long home_node, unsigned long flags); +asmlinkage long sys_cachestat(unsigned int fd, off_t off, size_t len, + size_t cstat_size, struct cachestat __user *cstat, unsigned int flags); /* * 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..fe03ed0b7587 100644 --- a/include/uapi/linux/mman.h +++ b/include/uapi/linux/mman.h @@ -4,6 +4,7 @@ #include #include +#include #define MREMAP_MAYMOVE 1 #define MREMAP_FIXED 2 @@ -41,4 +42,12 @@ #define MAP_HUGE_2GB HUGETLB_FLAG_ENCODE_2GB #define MAP_HUGE_16GB HUGETLB_FLAG_ENCODE_16GB +struct cachestat { + __u64 nr_cache; + __u64 nr_dirty; + __u64 nr_writeback; + __u64 nr_evicted; + __u64 nr_recently_evicted; +}; + #endif /* _UAPI_LINUX_MMAN_H */ diff --git a/init/Kconfig b/init/Kconfig index 694f7c160c9c..ecc4f781dd6c 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -1798,6 +1798,16 @@ config RSEQ If unsure, say Y. +config CACHESTAT + bool "Enable cachestat() system call" if EXPERT + default y + help + Enable the cachestat system call, which queries the page cache + statistics of a file (number of cached pages, dirty pages, + pages marked for writeback, (recently) evicted pages). + + If unsure say Y here. + config DEBUG_RSEQ default n bool "Enabled debugging of rseq() system call" if EXPERT 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..61c9c3f81cef 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -138,3 +138,4 @@ obj-$(CONFIG_IO_MAPPING) += io-mapping.o obj-$(CONFIG_HAVE_BOOTMEM_INFO_NODE) += bootmem_info.o obj-$(CONFIG_GENERIC_IOREMAP) += ioremap.o obj-$(CONFIG_SHRINKER_DEBUG) += shrinker_debug.o +obj-$(CONFIG_CACHESTAT) += cachestat.o diff --git a/mm/cachestat.c b/mm/cachestat.c new file mode 100644 index 000000000000..931f5e37326b --- /dev/null +++ b/mm/cachestat.c @@ -0,0 +1,117 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +/* + * The cachestat() system call. + */ +#include +#include +#include +#include +#include +#include +#include + +#include "swap.h" + +/* + * The cachestat(5) system call. + * + * cachestat() returns the page cache status of a file in the + * bytes specified by `off` and `len`: number of cached pages, + * number of dirty pages, number of pages marked for writeback, + * number of (recently) evicted pages. + * + * `off` and `len` must be non-negative integers. If `len` > 0, + * the queried range is [`off`, `off` + `len`]. If `len` == 0, + * we will query in the range from `off` to the end of the file. + * + * `cstat_size` allows users to obtain partial results. The syscall + * will copy the first `csstat_size` bytes to the specified userspace + * memory. It also makes the cachestat struct extensible - new fields + * can be added in the future without breaking existing usage. + * `cstat_size` must be a non-negative value that is no larger than + * the current size of the cachestat struct. + * + * The `flags` argument is unused for now, but is included for future + * extensibility. User should pass 0 (i.e no flag specified). + * + * 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_DEFINE6(cachestat, unsigned int, fd, off_t, off, size_t, len, + size_t, cstat_size, struct cachestat __user *, cstat, + unsigned int, flags) +{ + struct fd f; + struct cachestat cs; + + memset(&cs, 0, sizeof(struct cachestat)); + + if (off < 0 || cstat_size > sizeof(struct cachestat) || flags != 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; + pgoff_t last_index = + len == 0 ? ULONG_MAX : (off + len - 1) >> PAGE_SHIFT; + XA_STATE(xas, &mapping->i_pages, first_index); + struct folio *folio; + + rcu_read_lock(); + xas_for_each(&xas, folio, last_index) { + if (xas_retry(&xas, folio) || !folio) + continue; + + if (xa_is_value(folio)) { + /* page is evicted */ + void *shadow = (void *)folio; + bool workingset; /* not used */ + + cs.nr_evicted += 1; + +#ifdef CONFIG_SWAP /* implies CONFIG_MMU */ + 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); + } +#endif + 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, cstat_size)) + return -EFAULT; + + return 0; + } + + return -EBADF; +} From patchwork Thu Dec 8 20:28:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nhat Pham X-Patchwork-Id: 13068847 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 17312C4332F for ; Thu, 8 Dec 2022 20:28:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9F5D58E0008; Thu, 8 Dec 2022 15:28:17 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9A36A8E0009; Thu, 8 Dec 2022 15:28:17 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 82C368E0001; Thu, 8 Dec 2022 15:28:17 -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 743CC8E0001 for ; Thu, 8 Dec 2022 15:28:17 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 4C54280C73 for ; Thu, 8 Dec 2022 20:28:17 +0000 (UTC) X-FDA: 80220276234.15.76A9F07 Received: from mail-pg1-f175.google.com (mail-pg1-f175.google.com [209.85.215.175]) by imf24.hostedemail.com (Postfix) with ESMTP id 9F82118000F for ; Thu, 8 Dec 2022 20:28:15 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=N3M8dzC7; spf=pass (imf24.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.215.175 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=1670531295; 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=gHlLt6aWAsdCI5Y5mp+WVK1u7mkTRlfHIjBLtpnt7a4=; b=4l2LBIOD8zkXEs1J/0MTICaRUjmg0P0st0L+rUOwGt4rWKx3tixk7LCYp+S5BZy1nJWf9j B6kVYFxW8UQ4xIGVfErZtQfua0/DKcBvrFv52l/S9++HDklg89X2WTSmEj7Kit/zonmEIm VdbV6reGIkISLJFz/Nn8CqPzteCP4so= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=N3M8dzC7; spf=pass (imf24.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.215.175 as permitted sender) smtp.mailfrom=nphamcs@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1670531295; a=rsa-sha256; cv=none; b=sKzyJteGvpZdft0qUeDpXEUPEJXxOv519hpRLztqLYrDHe3163HoVHi6C+N/Zby5ORwizI m4A5/j8TZKjWpVMNwtLp7DZckDJKbar7yi0PXUonBLUpPiN80SNNqBroRQI3s66PpWcPhb 7KL2adZaheiLL7b9eIYjv0TGaReMe0M= Received: by mail-pg1-f175.google.com with SMTP id r18so2072938pgr.12 for ; Thu, 08 Dec 2022 12:28:15 -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=gHlLt6aWAsdCI5Y5mp+WVK1u7mkTRlfHIjBLtpnt7a4=; b=N3M8dzC7SB5Ru5QsrRLNYoBMAzhR8GmN/iwfvDMnqWpLxUQ5lP8RABY9JHwziygBBP FfebVYUGm7o42AdWO7a6gOy6/5S+3SVNTwZQYJv4pREi/ZajSGsx16mpLlaDQwPl8VtK 8Q0/Jo84C8fY43oYVQdBs8TlhKUZItSM5kPU/5htVdLs9zzKli92jymYGstdBKNAam/1 bLeG9o5iXIW1iIoIEHYw1fJ4a312pEXcoDtZ0Mc/LrAl6cG1vdQWD79ateZ1I00fpmuS mPbCEQ2LbkFZIN/7mNC7C1MZKZVduua5Bf1XH/0aY7QSS5TjAqUwFw+wc+YWwkjDU1rz Di4Q== 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=gHlLt6aWAsdCI5Y5mp+WVK1u7mkTRlfHIjBLtpnt7a4=; b=INufSvfxwJvuMuHVgAzPqFx2OdUOCsCvnlhdEgWHhS2A9K3OCAK6BD5OtJpcATP67x WVC3vR6MolIsz7lVW3u5jrUWEWSHh80iLtrAfW1lJp/gUbfTO4r5w3yRaUoznx4ZF2Jz Jcja9lF4l3SPCwqLjtSjLTAF3OOQd/dcaahG9Sue0j6hUFSep+K7HRXVAoLyKJ5uxRr2 I9Abm2FrbB6NddBxWTDcpcSX4eEa2qa2dYgNNMm6rAitCoZQru8LdF+jcB7fGxddbjyk GJUtIJfV90U+choiaYBSrVeYO9urCsPcO/t/HBI8BYZ9x9DfkFiD4NFHNY0Ju0x349CT DE0g== X-Gm-Message-State: ANoB5pkpjvFogKpjPIDmlfFdY4O6SKVs3iDwtmCx3p9BW3HDjRMxP7fz Y8J7XIodj0WNJE4Mk9ZlPxQ= X-Google-Smtp-Source: AA0mqf7bB1fsNiUIfGt54Z28MHiEvcKog5d25ZWm3PH+b8dbVs0v+JXrmwc6bMqO4MSlV0D5vUHxMQ== X-Received: by 2002:a62:2702:0:b0:572:8766:598b with SMTP id n2-20020a622702000000b005728766598bmr77349901pfn.21.1670531294542; Thu, 08 Dec 2022 12:28:14 -0800 (PST) Received: from localhost (fwdproxy-prn-014.fbsv.net. [2a03:2880:ff:e::face:b00c]) by smtp.gmail.com with ESMTPSA id e8-20020a17090301c800b00189b2b8dbedsm15105213plh.228.2022.12.08.12.28.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Dec 2022 12:28:14 -0800 (PST) From: Nhat Pham To: akpm@linux-foundation.org Cc: hannes@cmpxchg.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, bfoster@redhat.com, willy@infradead.org, kernel-team@meta.com Subject: [PATCH v3 4/4] selftests: Add selftests for cachestat Date: Thu, 8 Dec 2022 12:28:08 -0800 Message-Id: <20221208202808.908690-5-nphamcs@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20221208202808.908690-1-nphamcs@gmail.com> References: <20221208202808.908690-1-nphamcs@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 9F82118000F X-Stat-Signature: 74ijkzuz7ao9jie79fwzixd5ij8t7gub X-HE-Tag: 1670531295-631859 X-HE-Meta: U2FsdGVkX19yF3V9SOsfjG7H6jKpPvb2dsJCAJ0TW8LDRYrdRX1G90Tf8DuR72WQatljC0WcrzfcJSn+gtpD/zIrbuhtATm33WQXZGjRHZ2StuXhlVBLIFp3pdxsnxIMxiErEzxgpE24DNBFju88bdN1x9OhEawmUCtqKgNlKb2DK5XilbRsA8Y9ZnQb7QD+v+eL1n034WXeF6fxd1lS3Toh/WsZc5fZvMOWN+pge3Ruy6UxRUm5f10N64eeiUSKc/pjU5j+8Qmjlt4dHAXrPySzbN5ePQ21FFR3rFIw1u0woo1jALt9ESCW0IDWmlspzbgT7ZOAp2rAltz9yqXDSDXss+5zi7Rb5DyctbR3T2DOIRhWTR0l/20ngdbdug+NUroQY0lOC54ddloBGR7QAscbEydKilXlIRwVmuzAY9iXyPp+jIR/aRR3+DCsDqoKt47tLMxYj5OoiAZ1QebKJALApVBCvl/LleZ+Ei6vjgUTF/PMb7NXCc51CPzSgtCWRzXfucEGcGkwqRgJrsvVIiQjdY8LGsxh5gyM+ShqQbOQa/uKv37nusFrYjG2TXoSKBhbkpoFI1XoYmclagDlm71xG/rbW2NxuheZ3nEqZd1yosUfgKAYzJbZ82Wz0EaQ7M3K0s4gIL6oUlTHX69OzIanSGsaLp+R+9gSYfMgugOLEfNK5KzMP/oiEDeIpGaZZZc9FoPJ+EDcRDUJwhTc6bMkxPlsFKVJvtx6kMHGl6s8uGggq1Qv/z7rKnEQLSEWbyzFPy6mGqC8Y5BmMrbWxQKg++Je9oCRIwhxryQuenBV8VcUVvkJudpi5yW+j+OfPtFTf13/rvGGI0KlxHcIir1XyNXqOeEZ0dmCN3jq3lc5YVB2C7L+nH4lZIqhFfz6C8hwYJRdtG+ma6u4yao3s4GXhOK6X+qLmkLFhyDsYnj0fp0XFA5AKwLqnPifO3QH7X7sLD6P4mm2XNlgSPJ rCSzHqxt TRQ2vCNA+5IRj5WFxK2WP7epxFr+GdKj71XbeP8660QFQhhO8f1Gufb4MxfMlxL4Kyka5jYzr+6kZYK0AaEVZeTTEsnpbTziZGX2oePjVjpVNjLbaJ3BwSNLBST3in1BJNScc X-Bogosity: Ham, tests=bogofilter, spamicity=0.000011, 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 | 7 + .../selftests/cachestat/test_cachestat.c | 186 ++++++++++++++++++ 5 files changed, 197 insertions(+) create mode 100644 tools/testing/selftests/cachestat/.gitignore create mode 100644 tools/testing/selftests/cachestat/Makefile create mode 100644 tools/testing/selftests/cachestat/test_cachestat.c -- 2.30.2 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..04e84fd7d8ce --- /dev/null +++ b/tools/testing/selftests/cachestat/Makefile @@ -0,0 +1,7 @@ +# SPDX-License-Identifier: GPL-2.0 +TEST_GEN_PROGS := test_cachestat + +CFLAGS += $(KHDR_INCLUDES) +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..bb767147d08f --- /dev/null +++ b/tools/testing/selftests/cachestat/test_cachestat.c @@ -0,0 +1,186 @@ +// SPDX-License-Identifier: GPL-2.0 +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../kselftest.h" + +static const char * const dev_files[] = { + "/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, + sizeof(struct cachestat), &cs, 0); + + 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, + sizeof(struct cachestat), &cs, 0); + + 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; +}