From patchwork Fri Feb 3 19:04:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nhat Pham X-Patchwork-Id: 13128275 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 B12DEC05027 for ; Fri, 3 Feb 2023 19:04:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D42886B0072; Fri, 3 Feb 2023 14:04:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CF1BB6B0074; Fri, 3 Feb 2023 14:04:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B93D96B0075; Fri, 3 Feb 2023 14:04:18 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id A7AAC6B0072 for ; Fri, 3 Feb 2023 14:04:18 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 755D1140A35 for ; Fri, 3 Feb 2023 19:04:18 +0000 (UTC) X-FDA: 80426906196.15.93576D9 Received: from mail-pg1-f173.google.com (mail-pg1-f173.google.com [209.85.215.173]) by imf02.hostedemail.com (Postfix) with ESMTP id 2188A80004 for ; Fri, 3 Feb 2023 19:04:14 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b="WMo/4w24"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf02.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.215.173 as permitted sender) smtp.mailfrom=nphamcs@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1675451055; 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:references:dkim-signature; bh=DIAAb3JvVFe/JfZ06ePgE24x4hDheu1T0aof9KbX310=; b=7HGH+9jR8X0JCPtbVhQ1d+NGVx0w2m0Io+dJXf+21+q+LvOphBgPIwovJya9RgSxQrcfpq UIxEoEPGz9b3u3MbumS2pPq3ZVHzoGy1cPYheYEnLMmoUIbgeI6ppnc9ee5eQuCfhD9F2i KeU1+h9oqbmRMQID5Qj9AcWVlk22zlI= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b="WMo/4w24"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf02.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.215.173 as permitted sender) smtp.mailfrom=nphamcs@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1675451055; a=rsa-sha256; cv=none; b=6TE7lpLMKtFM8FEjYnCSBEeNKMHu2HMrIYruWE5m8y0xHp22U9NjxOYDAZHXnAMn8+2TWj WfkmR6Uox2EpjnarEmhrFgqW3LPeHL73uc1T2j/dUEdUT/XGWPH9TWyXuZoZM0YzM1uuxH wOCq2F4RxRv1L/q3+8Ej+h0PJpJlgRU= Received: by mail-pg1-f173.google.com with SMTP id s8so2173547pgg.11 for ; Fri, 03 Feb 2023 11:04:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=DIAAb3JvVFe/JfZ06ePgE24x4hDheu1T0aof9KbX310=; b=WMo/4w24GYrb4HwP1ubPwTJP3051q2dr+NVDc3cZr0WlOU4mGKpSTUbi5LDkfukrgW aAGrEzlk78FA4eCqya8LWuekE8NknLB3dgRACuC6HrTvyfp3llabXmaQSowrkFMEF37h MQQB3Q0BtCNrtPClyT8lkW64f77ebYcwKyOZ7TO/Wf4aCkJnx6TSjc3gp1lDmf8ZEhsf 18F5YVxvbFZhcbdIjqf7LRr7X4eAXWCS9Jt1wXjqAYsDvoN3Is/Jd3H8t5TV3/YF8jnF UG0uMP4PGO4eKpqSbN8xYineXBCzW8K+mY2NaE8pv6oSH6fZJtlYOkFLgmJ5pankdfMe C0dg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=DIAAb3JvVFe/JfZ06ePgE24x4hDheu1T0aof9KbX310=; b=NpdWm1F6XSZ2UD14if40iIojJ8ZC8N97993/rVY6jy+yt7xcKBgU47a1naMou8KtYu 0Ce3zc/sTeF0qExAJWLGxfIfrZHOURmczh23PfoTeZ5SYOctMmCIBCprSwy3mF7vovAN 4/ERjmizIHOODrZVAm1lwMRUhu83f8ElIAbrAOhINwCFmvVFi0cG9+yBLcbh67TFzkd4 S8BnSd3+4Fxs8JmWc89sruSW9klqQn9lw1Q4lxtb6+JzvOkRJT9blA/aYxE4NOVUp7sJ ykeHicAk9dzDL0Dd0hx5JlWF1GKdFl43MzVmXYyaXUOhCLlwuTKLS4aE6LHu3xSQaHiO LgmQ== X-Gm-Message-State: AO0yUKXNeD4sUrR7r0BNDuONayoLa799EVQlpdLiYc4rZKlZrxu9lxHO Tkx+phWdwKPqS/11G0KpBRU= X-Google-Smtp-Source: AK7set+r7dfs0c/MDPthFFRe7IUNHS2fK5o5BUDku/0fi1MAqsZRY73i5rw/OzcQPnW+X4B8H/Wckg== X-Received: by 2002:a05:6a00:1a47:b0:593:b309:aa55 with SMTP id h7-20020a056a001a4700b00593b309aa55mr11541388pfv.28.1675451053897; Fri, 03 Feb 2023 11:04:13 -0800 (PST) Received: from localhost (fwdproxy-prn-001.fbsv.net. [2a03:2880:ff:1::face:b00c]) by smtp.gmail.com with ESMTPSA id q3-20020a056a00084300b00593adee79efsm2184362pfk.55.2023.02.03.11.04.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Feb 2023 11:04:13 -0800 (PST) From: Nhat Pham To: akpm@linux-foundation.org Cc: hannes@cmpxchg.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, bfoster@redhat.com, willy@infradead.org, linux-api@vger.kernel.org, kernel-team@meta.com Subject: [PATCH v9 0/3] cachestat: a new syscall for page cache state of files Date: Fri, 3 Feb 2023 11:04:10 -0800 Message-Id: <20230203190413.2559707-1-nphamcs@gmail.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 2188A80004 X-Stat-Signature: hfqxbkcshiyp6at1odu7o88t1azgtzzq X-HE-Tag: 1675451054-359030 X-HE-Meta: U2FsdGVkX1+7P8HWxUVPg5ZNmJl+qvIYrnfjrs5Wpdop8sP2jblaHC1sGwg3Ffs6o3TRhqLQIznLONLChN7etEkYjvJOFZ1ptOjR/p82gsHcleJcy2mPZmyNST8WtQLzp3JCYCn460ohbpBLBAM+oBMWK3rAewPZfZCLkr1lSccVroEDn9160YPk3k3N8pMY8SxuTWaI492AYYJQtMJGNzKDnqRbNyp7feVwZ1QSRXEbC5hVkCWJtCEqVpBlRyVsYFXV7ByI2KKWwoc6bAU9prPMVeH897y1tmTlU9CV3gSy8IG0GbLH/EgRD9FvOskToeGOfKOEd3lzN9VEQ40VMSmwIKWWPWhnHkg0+lDWwciNZeydhhZwCaai3ag0ElkhJEkOWpDV/keH3tr7qB00WHwOHpTJuZwXSuttnY1foKU9zvruLfaOiThjCdJNDw/1Kaj5VitcFUvFd7Y5d+WkYKlCMnIQQMQ5YGgcDc6bqoayw7uMwp62Y6sBTljaiYLoUUZhjnhIC8BZ4zLLrvuknq8eBkwI6pDIunUkgEYXOWtTZNF8nUcD44hIfUMOvsp7UwgPE3zy38NWKfUL34tPREem7XmQeAHug6FFKilKiSocYwAXtQcfYtfxBWmQd/X/6ML+oHeS4mr+0u2X5tb7Nx5RgDAYIcaRrF1IRNWWsCqNCGaQFgWoP5kf4WQRwbiEZRVJkouXTeXiG/AvJxIVLmootUTPSFKZUGEqjwPDQDdWJAeZkX8f1VBYeYKBxWrQj6AYq9ZJGDbqApm78M3MEJ/u0Sfzk5F/ESLW/JrzpKiX3gOjaZzvxVkaLKh3WtPpYKQtcbRkdfntqCAmKNxlGgnivy8DjkS83MthfJrVzrmxKsZuhkfKrpPxcYDr+NyPL0Md//m/hCqmQvp1QdjsGusdcOiKufFRD4aXpYAtDFQGp30tKrz5OkXHRFJv0dFiyLL+eI9FBPsV8fBEs06 v5lpwrFW fYif4/FK65JKXrtxohxehrKsSDW3hpjl/AbUOQS4p0d5rAfrjrxLsRFLiIDKPLJLR6bpkgZ7WSTs9ecm8Zh1cy/wpV6QH8JaO+RNleouwl460YJXyJzDWlaRGPT9rk7U+XETS5TqjbMowg+IdyyoTFRcKhixGcdiOoh6Vgrn+wu+Cwc1NW/XTyeHQMd33DkujuMjTh5YTQF6d/SK5Ga7cjA9L/e65Xz80iw1U7JDFKR+RvCW3hG+Qee37pdeRVl6/aNsw6Tvd95YMpB/Av+I4BdoRRFtCXfCohfIGC3xH6JdW6acR0VcmpZj67YvrYOmxy7N1fjYYVszkK/Thp5FFIVCumXrVLbCdRymaMajCdtx5vi0MYix0N9/oGTD/Qr3Ap9k/mIXQOLFR5LvbIj2S3PHvtHFEppfqWSQKvHTtaknrmQaY4VlT2v5SJHhBZ1I5AIMP/ggbJuAbiu1WG7C6Zwom9Bc//rXuxtW8hvdmlVGhc/vugCQmN1vUbYy3L+k+FbIPn8ATSV6Hds8fbL74umjtGUduyuHEW+epwfsOadZNn6s= 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: Changelog: v9: * Remove syscall from all the architectures syscall table except x86 (patch 2) * API change: handle different cases for offset and add compat syscall. (patch 2) (suggested by Johannes Weiner and Arnd Bergmann) v8: * Add syscall to mips syscall tables (detected by kernel test robot) (patch 2) * Add a missing return (suggested by Yu Zhao) (patch 2) v7: * Fix and use lru_gen_test_recent (suggested by Brian Foster) (patch 2) * Small formatting and organizational fixes v6: * Add a missing fdput() (suggested by Brian Foster) (patch 2) * Replace cstat_size with cstat_version (suggested by Brian Foster) (patch 2) * Add conditional resched to the xas walk. (suggested by Hillf Danton) (patch 2) v5: * Separate first patch into its own series. (suggested by Andrew Morton) * Expose filemap_cachestat() to non-syscall usage (patch 2) (suggested by Brian Foster). * Fix some build errors from last version. (patch 2) * Explain eviction and recent eviction in the draft man page and documentation (suggested by Andrew Morton). (patch 2) v4: * Refactor cachestat and move it to mm/filemap.c (patch 3) (suggested by Brian Foster) * Remove redundant checks (!folio, access_ok) (patch 3) (suggested by Matthew Wilcox and Al Viro) * Fix a bug in handling multipages folio. (patch 3) (suggested by Matthew Wilcox) * Add a selftest for shmem files, which can be used to test huge pages (patch 4) (suggested by Johannes Weiner) v3: * Fix some minor formatting issues and build errors. * Add the new syscall entry to missing architecture syscall tables. (patch 3). * Add flags argument for the syscall. (patch 3). * Clean up the recency refactoring (patch 2) (suggested by Yu Zhao) * Add the new Kconfig (CONFIG_CACHESTAT) to disable the syscall. (patch 3) (suggested by Josh Triplett) v2: * len == 0 means query to EOF. len < 0 is invalid. (patch 3) (suggested by Brian Foster) * Make cachestat extensible by adding the `cstat_size` argument in the syscall (patch 3) There is currently no good way to query the page cache state of large file sets and directory trees. There is mincore(), but it scales poorly: the kernel writes out a lot of bitmap data that userspace has to aggregate, when the user really doesn not care about per-page information in that case. The user also needs to mmap and unmap each file as it goes along, which can be quite slow as well. This series of patches introduces a new system call, cachestat, that summarizes the page cache statistics (number of cached pages, dirty pages, pages marked for writeback, evicted pages etc.) of a file, in a specified range of bytes. It also include a selftest suite that tests some typical usage. Currently, the syscall is only wired in for x86 architecture. This interface is inspired by past discussion and concerns with fincore, which has a similar design (and as a result, issues) as mincore. Relevant links: https://lkml.indiana.edu/hypermail/linux/kernel/1302.1/04207.html https://lkml.indiana.edu/hypermail/linux/kernel/1302.1/04209.html For comparison with mincore, I ran both syscalls on a 2TB sparse file: Using mincore: real 0m37.510s user 0m2.934s sys 0m34.558s Using cachestat: real 0m0.009s user 0m0.000s sys 0m0.009s This series should be applied on top of: workingset: fix confusion around eviction vs refault container https://lkml.org/lkml/2023/1/4/1066 This series consist of 3 patches: Nhat Pham (3): workingset: refactor LRU refault to expose refault recency check cachestat: implement cachestat syscall selftests: Add selftests for cachestat MAINTAINERS | 7 + arch/x86/entry/syscalls/syscall_32.tbl | 1 + arch/x86/entry/syscalls/syscall_64.tbl | 1 + include/linux/compat.h | 4 +- include/linux/fs.h | 3 + include/linux/swap.h | 1 + include/linux/syscalls.h | 4 + include/uapi/asm-generic/unistd.h | 5 +- include/uapi/linux/mman.h | 9 + init/Kconfig | 10 + kernel/sys_ni.c | 1 + mm/filemap.c | 157 +++++++++++ mm/workingset.c | 142 ++++++---- tools/testing/selftests/Makefile | 1 + tools/testing/selftests/cachestat/.gitignore | 2 + tools/testing/selftests/cachestat/Makefile | 8 + .../selftests/cachestat/test_cachestat.c | 256 ++++++++++++++++++ 17 files changed, 564 insertions(+), 48 deletions(-) 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 base-commit: 1440f576022887004f719883acb094e7e0dd4944 prerequisite-patch-id: 171a43d333e1b267ce14188a5beaea2f313787fb